Linear Layout
LinearLayout是一个排列子元素单一方向(横向或纵向)的view组。你可以通过android:orientation属性指定一个布局方向。
![](https://img-my.csdn.net/uploads/201301/27/1359277732_4676.png)
LinearLayout的所有子元素一个接一个的堆放在一起,因此一个纵向列表只有一个任意宽度的列,一个横向的布局只有一行(他的高度取决于他最高的子元素的高的和填充部分的和)。LinearLayout的边距由他的子元素和每个子元素在该容器内的位置决定(中心、靠右、左对齐)。
Layout Weight
LinearLayout 也可以通过 android:layout_weight属性为他的子元素指定重要性,为这个属性分配一个重要性的值来决定一个元素在屏幕中占多大的空间。大的weight属性值可以使组件扩充多余的空间充满父组件。子元素可以分配一个Weight属性值,剩余的空间根据每个元素的Weight值来按比例分配给每个元素。默认的Weight是0。
假如现在有三个TextView元素,其中两个的weight属性为“1”,另外一个没有指定weight属性,第三个元素是不能进行扩展的,他占有的空间需要根据他的内容而定。其他的两个元素将平分剩余的空间。如果指定第三个元素的weight为“2”,则他将占据总空间的一半,另外的两个元素平分剩余的空间。
Example
<?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:paddingLeft="16dp" android:paddingRight="16dp" android:orientation="vertical" > <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/to" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/subject" /> <EditText android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="top" android:hint="@string/message" /> <Button android:layout_width="100dp" android:layout_height="wrap_content" android:layout_gravity="right" android:text="@string/send" /> </LinearLayout>
![](https://img-my.csdn.net/uploads/201301/27/1359277812_9928.png)