您好!欢迎来到源码码网

Android五大布局之线性布局LinearLayout使用

  • 源码教程
  • 来源:源码码网
  • 编辑:admin
  • 时间:2021-01-08 13:08
  • 阅读:492

LinearLayout 又称线性布局,该布局应该是 Android 视图设计中最经常使用的布局。该布局可以使放入其中的组件以水平方式或者垂直方式整齐排列,通过 android:orientation 属性指定具体的排列方式,通过 weight 属性设置每个组件在布局中所占的比重。

实例 LinearLayoutDemo 演示了 LinearLayout 布局的使用方法,效果如图 3 所示。

图 3  LinearLayout 的布局效果

实例 LinearLayoutDemo 中的 strings.xml 文件代码如下:

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <resources>

  3.   <string name="app_name">LinearLayoutDemo</string>

  4.   <string name="red">red</string>

  5.   <string name="yellow">yellow</string>

  6.   <string name="green">green</string>

  7.   <string name="blue">blue</string>

  8.   <string name="row1">row one</string>

  9.   <string name="row2">row two</string>

  10.   <string name="row3">row three</string>

  11.   <string name="row4">row four</string>

  12. </resources>

实例 LinearLayoutDemo 中的布局文件 main.xml 代码如下:


  1. <?xml version="1.0" encoding="utf-8"?>


  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3.     android:layout_width="fill_parent"

  4.     android:layout_height="fill_parent"

  5.     android:orientation="vertical">


  6.     <LinearLayout

  7.         android:layout_width="fill_parent"

  8.         android:layout_height="fill_parent"

  9.         android:layout_weight="1"

  10.         android:orientation="horizontal">


  11.         <TextView

  12.             android:layout_width="wrap_content"

  13.             android:layout_height="fill_parent"

  14.             android:layout_gravity="center_horizontal"

  15.             android:layout_weight="1"

  16.             android:background="#aa0000"

  17.             android:text="@string/red" />


  18.         <TextView

  19.             android:layout_width="wrap_content"

  20.             android:layout_height="fill_parent"

  21.             android:layout_gravity="center_horizontal"

  22.             android:layout_weight="1"

  23.             android:background="#00aa00"

  24.             android:text="@string/green" />


  25.         <TextView

  26.             android:layout_width="wrap_content"

  27.             android:layout_height="fill_parent"

  28.             android:layout_gravity="center_horizontal"

  29.             android:layout_weight="1"

  30.             android:background="#0000aa"

  31.             android:text="@string/blue" />


  32.         <TextView

  33.             android:layout_width="wrap_content"

  34.             android:layout_height="fill_parent"

  35.             android:layout_gravity="center_horizontal"

  36.             android:layout_weight="1"

  37.             android:background="#aaaa00"

  38.             android:text="@string/yellow" />

  39.     </LinearLayout>


  40.     <LinearLayout

  41.         android:layout_width="fill_parent"

  42.         android:layout_height="fill_parent"

  43.         android:layout_weight="1"

  44.         android:orientation="vertical">


  45.         <TextView

  46.             android:layout_width="fill_parent"

  47.             android:layout_height="wrap_content"

  48.             android:layout_weight="1"

  49.             android:text="@string/row1"

  50.             android:textSize="15pt" />


  51.         <TextView

  52.             android:layout_width="fill_parent"

  53.             android:layout_height="wrap_content"

  54.             android:layout_weight="1"

  55.             android:text="@string/row2"

  56.             android:textSize="15pt" />


  57.         <TextView

  58.             android:layout_width="fill_parent"

  59.             android:layout_height="wrap_content"

  60.             android:layout_weight="1"

  61.             android:text="@string/row3"

  62.             android:textSize="15pt" />


  63.         <TextView

  64.             android:layout_width="fill_parent"

  65.             android:layout_height="wrap_content"

  66.             android:layout_weight="1"

  67.             android:text="@string/row4"

  68.             android:textSize="15pt" />

  69.     </LinearLayout>

  70. </LinearLayout>

该布局中放置了两个 LinearLayout 布局对象。

第一个 LinearLayout 布局通过 android:orientation="horizontal" 属性将布局设置为横向线性排列。

第二个 LinearLayout 布局通过 android:orientation="vertical" 属性将布局设置为纵向线性排列。

每个 LinearLayout 布局中都放入了 4 个 TextView,并通过 android:layout_weight 属性设置每个组件在布局中所占的比重相同,即各组件大小相同。

layout_weight 用于定义一个线性布局中某组件的重要程度。所有的组件都有一个 layout_weight 值,默认为 0。意思是需要显示多大的视图就占据多大的屏幕空间。若赋值为大于 0 的值,则将可用的空间分割,分割的大小取决于当前的 layout_weight 数值与其他空间的 layout_weight 值的比率。

例如水平方向上有两个按钮,每个按钮的 layout_weight 数值都设置为 1,那么这两个按钮平分宽度;若第一个为 1,第二个为 2,则可将空间的三分之一分给第一个,三分之二分给第二个。

将 LinearLayoutDemo 中水平 LinearLayout 的第 4 个 TextView 的 android:layout_weight 属性赋值为 2,运行效果如图 4 所示。

修改android:layout_weight属性

图 4  修改 android:layout_weight 属性

LinearLayout 布局可使用嵌套。活用 LinearLayou 布局可以设计出各种各样漂亮的布局方式。

特别声明:
1、如无特殊说明,内容均为本站原创发布,转载请注明出处;
2、部分转载文章已注明出处,转载目的为学习和交流,如有侵犯,请联系客服删除;
3、编辑非《源码码网》的文章均由用户编辑发布,不代表本站立场,如涉及侵犯,请联系删除;
全部评论(0)
推荐阅读
  • 大型后台管理系统,用户登录状态该如何保存?
  • 大型后台管理系统,用户登录状态该如何保存?
  • 大型后台管理系统的用户登录状态保存需要综合考虑安全性、用户体验和系统架构。以下是企业级的完整方案:1.多层级存储策略class AuthManager {    constructor() {        this.storage = { &n
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2025-11-06 12:16
  • 阅读:350
  • 源码交易平台的支付困局与解决方案:如何通过专业支付系统提升交易效率
  • 源码交易平台的支付困局与解决方案:如何通过专业支付系统提升交易效率
  • 在数字经济蓬勃发展的今天,源码交易市场已成为互联网创业者和开发商的重要资源池。从电商系统源码到社交应用框架,从小程序解决方案到企业级管理系统,越来越多的开发者、初创企业和传统商家通过源码交易平台快速获取技术资产,实现商业目标的加速。源码交易市场的繁荣反映了数字化转型的迫切需求——企业需要快速迭代,开发者需要快速变现,用户需要快速启动。然而,在这个高速发展的市场中,一个长期被忽视但至关重要的问题浮现出来:支付系统的效率与安全性已成为制约交
  • 行业资讯
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2025-10-23 15:16
  • 阅读:298
  • Spring Boot 工程中 maven-surefire-plugin 测试执行失败及解决方法
  • Spring Boot 工程中 maven-surefire-plugin 测试执行失败及解决方法
  • 在SpringBoot工程编译时遇到maven-surefire-plugin的测试执行失败错误(Failedtoexecutegoalorg.apache.maven.plugins:maven-surefire-plugin:3.5.3:test),通常与测试环节相关。以下是常见原因及解决方法:1.测试用例执行失败• 原因:最常见的是测试用例(*Test.java)运行时抛出异常(如断言失败、空指针等),导
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2025-10-13 10:57
  • 阅读:363
  • WeMark - 微信小程序图片水印
  • WeMark - 微信小程序图片水印
  • 一个纯前端的微信小程序图片水印工具。支持文字/图片水印、单个与全屏两种模式,透明度与角度调节、单个水印位置X/Y控制,预览与对比模态、历史记录(100条)等功能。
  • 源码教程
  • 来源:源码码用户
  • 编辑:yg
  • 时间:2025-09-22 16:09
  • 阅读:287
联系客服
源码代售 源码咨询 素材咨询 联系客服
029-84538663
手机版

扫一扫进手机版
返回顶部