1. 程式人生 > >Layout resource(佈局資源)-----官網翻譯

Layout resource(佈局資源)-----官網翻譯

開發十年,就只剩下這套架構體系了! >>>   

佈局資源在UI的活動或元件中定義UI的體系結構。

file location:(檔案位置)

res/layout/filename.xml
檔名將用作資源ID

  • 編制資源資料型別:
    指向檢視(或子類)資源的資源指標。

  • 資源引用:
    In Java: R.layout.filename
    In 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>

    其他檢視元素的容器。有許多不同種類的ViewGroup物件,每一種都允許您以不同的方式指定子元素的佈局。不同型別的ViewGroup物件包括     LinearLayout、RelativeLayout和FrameLayout。 

    您不應該假定ViewGroup的任何派生都將接受巢狀檢視。有些檢視組是AdapterView類的實現,該類僅從介面卡確定其子類。

    <View>

    屬性:

    android:id

    資源ID。元素的唯一資源名,您可以使用它從應用程式中獲得對ViewGroup的引用。有關android:id的更多資訊,請參見下面。

    android:layout_height

    維度或關鍵字。必需的。元素的高度,作為維度值(或維度資源)或關鍵字(“match_parent”或“wrap_content”)。請參閱下面的有效值。

    android:layout_width

    維度或關鍵字。必需的。元素的寬度,作為維度值(或維度資源)或關鍵字(“match_parent”或“wrap_content”)。請參閱下面的有效值。

    檢視基類支援更多的屬性,檢視的每個實現支援更多的屬性。閱讀佈局以獲得更多資訊。有關所有可用屬性的引用,請參見相應的引用文件(例如TextView     XML屬性)。

    <requestFocus>

    一個單獨的UI元件,通常稱為“小部件”。不同型別的檢視物件包括TextView、Button和複選框。

    <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的更多資訊,請閱讀按需載入檢視。

  &nbs