本例介紹了LinearLayout 水平布局的基本用法:
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”horizontal”
android:layout_width=”match_parent”
android:layout_height=”match_parent”>
<TextView
android:background=”@drawable/red”
android:layout_width=”0dip”
android:layout_height=”match_parent”
android:layout_weight=”1″/>
<TextView
android:background=”@drawable/green”
android:layout_width=”0dip”
android:layout_height=”match_parent”
android:layout_weight=”1″/>
<TextView
android:background=”@drawable/blue”
android:layout_width=”0dip”
android:layout_height=”match_parent”
android:layout_weight=”1″/>
<TextView
android:background=”@drawable/yellow”
android:layout_width=”0dip”
android:layout_height=”match_parent”
android:layout_weight=”1″/>
</LinearLayout>
注意這裡四個TextView 的權重都設為1,因此將平均分配大小。使用權重的一個基本演算法,LinearLayout 剩餘的空間有所有有“權重”的View按照其權值按比例分配大小。