版面配置資源
    
    
      
    
    
      
      透過集合功能整理內容
    
    
      
      你可以依據偏好儲存及分類內容。
    
  
  
      
    
  
  
  
  
  
    
    
    
  
  
    
    
    
版面配置資源可定義 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>注意:根元素可以是 ViewGroup、View或<merge>元素,但全部只能有一個根元素,而且此根元素必須包含具備android命名空間的xmlns:android屬性,如上述語法範例所示。
 
- 元素:
- 
  
  - <ViewGroup>
- 其他 View元素的容器。ViewGroup物件的種類有很多,每個物件都可讓您以不同方式指定子項元素的版面配置。不同種類的ViewGroup物件包括LinearLayout、RelativeLayout和FrameLayout。請勿假設所有 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類別的對應參考文件,例如「LinearLayoutXML 屬性」。
 
- <View>
- 個別 UI 元件,通常稱為「小工具」。不同種類的 View物件包括TextView、Button和CheckBox。屬性: 
        - 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的各項實作又支援更多屬性。詳情請參閱「版面配置」。如需所有可用屬性的參考資料,請參閱對應參考文件,例如「TextViewXML 屬性)。
 
- <requestFocus>
- 任何代表 View物件的元素都可以包含此元素,此空元素會在螢幕畫面上為其父項提供初始焦點。每個檔案只能有一個這類元素。
- <include>
- 在此版面配置中加入版面配置檔案。
      屬性: 
        - layout
- 「版面配置資源」。必要。對版面配置資源的參照。
- android:id
- 「資源 ID」。在內附的版面配置中,覆寫根層級檢視畫面獲派的 ID。
        
- android:layout_height
- 「尺寸或關鍵字」。在內附的版面配置中,覆寫根層級檢視畫面獲派的高度。只有一併宣告 android:layout_width時才有作用。
- android:layout_width
- 「尺寸或關鍵字」。在內附的版面配置中,覆寫根層級檢視畫面獲派的寬度。只有一併宣告 android:layout_height時才有作用。
 在內附的版面配置中,您可以在根元素支援的 <include>內,加入任何其他版面配置屬性,這些屬性會覆寫根元素中定義的對應屬性。
 注意:如要使用 <include>標記覆寫版面配置屬性,您必須一併覆寫android:layout_height和android: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:
 Kotlinval textView: TextView? = findViewById(R.id.nameTextbox) JavaTextView 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 | 將尺寸設為符合此元素內容所需的大小。 |  
 自訂檢視畫面元素您可以建立自訂的 View和ViewGroup元素,然後按照套用標準版面配置元素的做法套用至版面配置中。您也可以指定 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的版面配置:
 
- 
Kotlinpublic override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.main_activity)
}Javapublic void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
}
- 另請參閱:
- 
 
    
  
  
    
      
      
    
    
      
    
    
  
       
    
    
      
    
  
  
  這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
  上次更新時間:2025-07-27 (世界標準時間)。
  
  
  
    
      [[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[]]