版面配置資源

版面配置資源定義了 Activity 中的 UI 架構,或 UI 元件的架構。

檔案位置:
res/layout/filename.xml
系統會把檔案名稱當做資源 ID。
編譯資源資料類型:
View (或子類別) 資源的資源指標。
資源參照:
Java:R.layout.filename
XML:@[package:]layout/filename
語法:
<?xml version="1.0" encoding="utf-8"?>
<ViewGroup
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@[+][package:]id/resource_name"
    android:layout_height=["dimension" | "match_parent" | "wrap_content"]
    android:layout_width=["dimension" | "match_parent" | "wrap_content"]
    [ViewGroup-specific attributes] >
    <View
        android:id="@[+][package:]id/resource_name"
        android:layout_height=["dimension" | "match_parent" | "wrap_content"]
        android:layout_width=["dimension" | "match_parent" | "wrap_content"]
        [View-specific attributes] >
        <requestFocus/>
    </View>
    <ViewGroup >
        <View />
    </ViewGroup>
    <include layout="@layout/layout_resource"/>
</ViewGroup>

注意:根元素可以是 ViewGroupView<merge> 元素,但只能有一個根元素,而且此根元素必須包含 xmlns:android 屬性及 android 命名空間,如上述語法範例所示。

元素:
<ViewGroup>
其他 View 元素的容器。ViewGroup 物件的種類有很多,每個物件都可讓您以不同方式指定子項元素的版面配置。不同種類的 ViewGroup 物件包括 LinearLayoutRelativeLayoutFrameLayout

請勿假設所有 ViewGroup 衍生物件都會接受巢狀檢視區塊。部分檢視區塊群組為 AdapterView 類別的實作,該類別只會從 Adapter 判斷其子項。

屬性:

android:id
「資源 ID」。元素的不重複資源名稱,可用於從應用程式取得對 ViewGroup 的參照。詳情請參閱「android:id 的值」一節。
android:layout_height
尺寸或關鍵字。必要。群組的高度,以尺寸值 (或尺寸資源) 或關鍵字 ("match_parent""wrap_content") 的形式設定。詳情請參閱「android:layout_height 和 android:layout_width 的值」一節。
android:layout_width
尺寸或關鍵字。必要。群組的寬度,以尺寸值 (或尺寸資源) 或關鍵字 ("match_parent""wrap_content") 的形式設定。詳情請參閱「android:layout_height 和 android:layout_width 的值」一節。

ViewGroup 基礎類別支援更多屬性,而且 ViewGroup 的每個實作又支援更多屬性。如需所有可用屬性的參考資料,請參閱 ViewGroup 類別的對應參考文件,例如「LinearLayout XML 屬性」。

<View>
個別 UI 元件,通常稱為「小工具」。不同種類的 View 物件包括 TextViewButtonCheckBox

屬性:

android:id
「資源 ID」。元素的不重複資源名稱,可用以從應用程式取得 View 的參照。詳情請參閱「android:id 的值」一節。
android:layout_height
尺寸或關鍵字。必要。元素的高度,以尺寸值 (或尺寸資源) 或關鍵字 ("match_parent""wrap_content") 的形式設定。詳情請參閱「android:layout_height 和 android:layout_width 的值」一節。
android:layout_width
尺寸或關鍵字。必要。元素的寬度,以尺寸值 (或尺寸資源) 或關鍵字 ("match_parent""wrap_content") 的形式設定。詳情請參閱「android:layout_height 和 android:layout_width 的值」一節。

View 基礎類別支援更多屬性,而且 View 的每個實作又支援更多屬性。詳情請參閱「版面配置」。如需所有可用屬性的參考資料,請參閱對應參考文件,例如「TextView XML 屬性」。

<requestFocus>
任何代表 View 物件的元素都可以包含此空白元素,為其父項提供畫面中的初始焦點。每個檔案只能有一個這類元素。
<include>
在此版面配置中加入版面配置檔案。

屬性:

layout
「版面配置資源」。必要。對版面配置資源的參照。
android:id
「資源 ID」。覆寫指派給內含版面配置中根層級檢視畫面的 ID。
android:layout_height
尺寸或關鍵字。覆寫指派給內含版面配置中根層級檢視畫面的高度。只有在已宣告 android:layout_width 的情況下才有效。
android:layout_width
尺寸或關鍵字。覆寫指派給內含版面配置中根層級檢視畫面的寬度。只有在已宣告 android:layout_height 的情況下才有效。

您可以在內含版面配置中根元素支援的 <include> 內,加入任何其他版面配置屬性,它們會覆寫根元素中定義的屬性。

注意:如要使用 <include> 標記覆寫版面配置屬性,您必須同時覆寫 android:layout_heightandroid:layout_width,其他版面配置屬性才會生效。

另一種加入版面配置的方法是使用 ViewStub:此為輕量型檢視畫面,在您明確加載此檢視畫面之前,它不會佔用任何版面配置空間。加載時它會加入由其 android:layout 屬性定義的版面配置檔案。如要進一步瞭解如何使用 ViewStub,請參閱「隨選載入檢視畫面」。

<merge>
沒有在版面配置階層繪製的額外根元素。如果您確定此版面配置將放到已包含適當父項 View 的版面配置中,以包含 <merge> 元素的子項,則將它做為根元素會十分實用。

如果要使用 <include>,在另一個版面配置檔案中加入此版面配置,而且此版面配置不需要使用不同的 ViewGroup 容器,這種做法會特別實用。如要進一步瞭解如何合併版面配置,請參閱「利用 <include> 重複使用版面配置」。

android:id 的值

通常 ID 值會使用 "@+id/name" 這類語法格式,如以下範例所示。加號 (+) 表示這是新的資源 ID,如果不存在資源整數,aapt 工具會在 R.java 類別中建立新的資源整數。

<TextView android:id="@+id/nameTextbox"/>

nameTextbox 名稱現在是此元素附加的資源 ID。接著,您就可以在 Java 中參照與此 ID 相關聯的 TextView

Kotlin

val textView: TextView? = findViewById(R.id.nameTextbox)

Java

TextView textView = findViewById(R.id.nameTextbox);

此程式碼會傳回 TextView 物件。

不過,如果您已定義 ID 資源,且該 ID 資源尚未使用,只要去掉 android:id 值中的加號,即可將該 ID 套用至 View 元素。

android:layout_height 和 android:layout_width 的值

高度和寬度值是使用 Android 支援的任何尺寸單位 (px、dp、sp、pt、in、mm) 或下列關鍵字來表示:

說明
match_parent 依照父項元素的維度設定維度。已在 API 級別 8 中新增,藉此淘汰 fill_parent
wrap_content 僅依照為符合此元素內容所需的大小設定維度。

自訂檢視畫面元素

您可以建立自訂的 ViewViewGroup 元素,然後將它們套用至版面配置,做法與套用標準版面配置元素相同。您也可以指定 XML 元素中支援的屬性。詳情請參閱「建立自訂檢視畫面元件」。

例如:
XML 檔案儲存在 res/layout/main_activity.xml
<?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:orientation="vertical" >
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello, I am a TextView" />
    <Button android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button" />
</LinearLayout>

此應用程式程式碼會在 onCreate() 方法中載入 Activity 的版面配置:

Kotlin

public override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.main_activity)
}

Java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
}
另請參閱: