版面配置資源

版面配置資源可定義活動或 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 衍生版本都會接受使用巢狀 View。有些 ViewGroupAdapterView 類別的實作,而且僅會透過 Adapter 判定其子項。

屬性:

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

ViewGroup 基礎類別支援更多屬性,而且 ViewGroup 的各個實作又支援更多屬性。如需所有可用屬性的參照,請參閱 ViewGroup 類別的對應參考文件 (例如:LinearLayout XML 屬性)。

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

屬性:

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

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>
沒有在版面配置階層繪製的額外根元素。如果您確定此版面配置將放置在已包含合適父項檢視畫面的版面配置中,以包含 <merge> 元素的子項,那麼使用此做為根元素就很實用。如果要使用 <include>,在另一個版面配置檔案中加入此版面配置,而且此版面配置不需要使用不同的 ViewGroup 容器,這種方式就特別實用。如要進一步瞭解如何合併版面配置,請參閱「利用 <include> 重複使用版面配置」。

android:id 的值

如果是 ID 值,通常都會使用以下語法格式:"@+id/name"。加號 (+) 表示這是新的資源 ID,而且 aapt 工具會在 R.java 類別中建立新的資源整數 (如果還沒有)。舉例來說:

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

nameTextbox 名稱現在是此元素附加的資源 ID。然後您就可以參照 TextView,且其 ID 在 Java 中會建立關聯:

Kotlin

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

Java

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

此程式碼會傳回 TextView 物件。

但是如果您已定義 ID 資源 (且該 ID 資源尚未使用),您就可以在 android:id 中排除加號,以套用該 ID至 View

android:layout_heightandroid: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);
}
另請參閱: