LinearLayout 又称线性布局,该布局应该是 Android 视图设计中最经常使用的布局。该布局可以使放入其中的组件以水平方式或者垂直方式整齐排列,通过 android:orientation……
LinearLayout 又称线性布局,该布局应该是 Android 视图设计中最经常使用的布局。该布局可以使放入其中的组件以水平方式或者垂直方式整齐排列,通过 android:orientation 属性指定具体的排列方式,通过 weight 属性设置每个组件在布局中所占的比重。
实例 LinearLayoutDemo 演示了 LinearLayout 布局的使用方法,效果如图 3 所示。
图 3 LinearLayout 的布局效果
实例 LinearLayoutDemo 中的 strings.xml 文件代码如下:
-
<?xml version="1.0" encoding="UTF-8"?>
-
<resources>
-
<string name="app_name">LinearLayoutDemo</string>
-
<string name="red">red</string>
-
<string name="yellow">yellow</string>
-
<string name="green">green</string>
-
<string name="blue">blue</string>
-
<string name="row1">row one</string>
-
<string name="row2">row two</string>
-
<string name="row3">row three</string>
-
<string name="row4">row four</string>
-
</resources>
实例 LinearLayoutDemo 中的布局文件 main.xml 代码如下:
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
android:orientation="vertical">
-
<LinearLayout
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
android:layout_weight="1"
-
android:orientation="horizontal">
-
<TextView
-
android:layout_width="wrap_content"
-
android:layout_height="fill_parent"
-
android:layout_gravity="center_horizontal"
-
android:layout_weight="1"
-
android:background="#aa0000"
-
android:text="@string/red" />
-
<TextView
-
android:layout_width="wrap_content"
-
android:layout_height="fill_parent"
-
android:layout_gravity="center_horizontal"
-
android:layout_weight="1"
-
android:background="#00aa00"
-
android:text="@string/green" />
-
<TextView
-
android:layout_width="wrap_content"
-
android:layout_height="fill_parent"
-
android:layout_gravity="center_horizontal"
-
android:layout_weight="1"
-
android:background="#0000aa"
-
android:text="@string/blue" />
-
<TextView
-
android:layout_width="wrap_content"
-
android:layout_height="fill_parent"
-
android:layout_gravity="center_horizontal"
-
android:layout_weight="1"
-
android:background="#aaaa00"
-
android:text="@string/yellow" />
-
</LinearLayout>
-
<LinearLayout
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
android:layout_weight="1"
-
android:orientation="vertical">
-
<TextView
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:layout_weight="1"
-
android:text="@string/row1"
-
android:textSize="15pt" />
-
<TextView
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:layout_weight="1"
-
android:text="@string/row2"
-
android:textSize="15pt" />
-
<TextView
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:layout_weight="1"
-
android:text="@string/row3"
-
android:textSize="15pt" />
-
<TextView
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:layout_weight="1"
-
android:text="@string/row4"
-
android:textSize="15pt" />
-
</LinearLayout>
-
</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 所示。
图 4 修改 android:layout_weight 属性
LinearLayout 布局可使用嵌套。活用 LinearLayou 布局可以设计出各种各样漂亮的布局方式。
还没有评论呢,快来抢沙发~