源码教程 2025年06月7日
0 收藏 0 点赞 560 浏览 3900 个字
摘要 :

LinearLayout 又称线性布局,该布局应该是 Android 视图设计中最经常使用的布局。该布局可以使放入其中的组件以水平方式或者垂直方式整齐排列,通过 android:orientation……

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

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

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

图 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五大布局之线性布局LinearLayout使用

图 4  修改 android:layout_weight 属性

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

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.zuozi.net/902.html

管理员

相关推荐
2025-07-05

对于一些使用WordPress进行外贸建站的商家来说,大部分人会通过在WordPress中添加JS代码和CSS样式表…

700
2025-07-05

当商家遇到WordPress独立站改版或者域名到期等问题时,不免会涉及到WordPress域名的更改,那么这一…

714
2025-07-05

用户在使用WooCommerce进行跨境电商独立站搭建工作时,有可能会借助WooCommerce短代码实现更加醒目…

306
2025-07-05

随着外贸建站行业的不断深入发展,WordPress的多语言功能也显得越来越重要,一个具有多语言的独立站…

1,038
2025-07-05

WooCommerce作为WordPress外贸建站生态的一部分,WooCommerce运费设置是商家在建站过程中不可忽视的…

835
2025-07-05

在外贸建站的过程中,有些商家会选择使用WordPress幻灯片为网站页面增加一定的动感和可观性,进而提…

722
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:08:00-23:00

客服QQ

122325244

客服电话

400-888-8888

客服邮箱

122325244@qq.com

扫描二维码

关注微信客服号