LinearLayout 翻譯過來就是「線性布局」:該標籤下的所有子元素,會依 orientation 屬性的取值決定是按行還是按列逐個排列。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/name_text" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cancle_button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ok_button" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, UIActivity!</string>
<string name="app_name">用戶界面</string>
<string name="name_text">請輸入用戶名</string>
<string name="ok_button">確定</string>
<string name="cancle_button">取消</string>
</resources>
· xmlns:android 指定命名空間,頂級元素必須聲明;該命名空間下的控件屬性都要加上 android: 前綴。
· layout_width 指定寬度,可取 fill_parent(填滿父容器,對頂級元素而言即整個屏幕)、wrap_content(僅包裹自身內容)或具體數值(單位 px)。
· layout_height 指定高度,取值含義與 layout_width 相同。
· orientation 指定排列方向:設為 vertical 時子元素垂直排列,每個佔獨立一行;另一個可選值 horizontal 則是水平排列,每個佔獨立一列。