Linear Layout
linear layout是一种view group,它以一个单一的方向(垂直或者是水平)排列它所有的子控件,你可以用android:orientation属性来指定布局方向。
linear layout所有的子控件都是一个个的堆叠在上一个子控件之后,所以如果一个linearlayout的方向是垂直的,那么在每一行只能有一个子控件,不管该控件有多宽。
Layout Weight
linear layout 可以通过android:layout_weight属性给每一个子控件分配一个weight。这个属性确定了每个子控件应该占据多少的屏幕空间。最大可以占据整个父控件所有的空间。父控件中的空间以子控件中声明的weight值的比例分配给子控件。默认的weight值是0。
例如:当三个button控件中的两个控件都声明了weight为1,另一个button没有给weight值,那么这个没有给定weight值的button控件就不会扩展,只是占据规定的位置,这两个给定weight值的控件就会扩展到完全占据剩余的空间。如果此时没有给定weight值的那个控件给定weight值是2,那么这三个button就按照1:1:2的比例来瓜分真个布局空间。
Example
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical" >
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/to" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/subject" />
<EditText
android:layout_width="match_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>
下面是是效果图: