所有的ViewGroup(Layout類的基類)都包含width 和height 屬性(layout_width 和layout_height),所有的View都必須定義這兩個值。
layout_width 和layout_height 允許使用絕對值來定義(如像素值),當為了使UI能夠自適應螢幕大小,一般不使用絕對值來定義View的寬度和高度。可以使用如下兩個常量:
wrap_content: 告訴View使用能包含其內容的尺寸 (類似於WinForm 中AutoSize)。
fill_parent(或match_parent 從Level 8起): 子View擴充為其父容器尺寸。
本例和上例的不同之處在於本例為LinearLayout設定了match_parent ,因此此LinearLayout將充滿螢幕:
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:background=”@drawable/blue”
android:layout_width=”match_parent”
android:layout_height=”match_parent”>
<!– view1 goes on top –>
<TextView
android:background=”@drawable/box”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”@string/linear_layout_2_top”/>
<!– view2 goes in the middle –>
<TextView
android:background=”@drawable/box”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”@string/linear_layout_2_middle”/>
<!– view3 goes on the bottom –>
<TextView
android:background=”@drawable/box”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”@string/linear_layout_2_bottom”/>
</LinearLayout>