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

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

file location:(文件位置)java

res/layout/filename.xml
文件名將用做資源IDandroid

  • 編制資源數據類型:
    指向視圖(或子類)資源的資源指針。ide

  • 資源引用:
    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>

    原理:ui

    <ViewGroup>this

    其餘視圖元素的容器。有許多不一樣種類的ViewGroup對象,每一種都容許您以不一樣的方式指定子元素的佈局。不一樣類型的ViewGroup對象包括     LinearLayout、RelativeLayout和FrameLayout。 google

    您不該該假定ViewGroup的任何派生都將接受嵌套視圖。有些視圖組是AdapterView類的實現,該類僅從適配器肯定其子類。spa

    <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的更多信息,請閱讀按需加載視圖。

    <merge>

    佈局層次結構中沒有繪製的替代根元素。當您知道這個佈局將被放置到一個已經包含適當父視圖的佈局中,以包含<merge>元素的子元素時,將它用做根元素很是有用。當您計劃使用<include>將該佈局包含在另外一個佈局文件中,而且該佈局不須要不一樣的ViewGroup容器時,這尤爲有用。有關合並佈局的更多信息,請閱讀使用<include/>重用佈局。

   Value for android:id連接

    對於ID值,一般應該使用這種語法形式:「@+ ID /name」。加號+表示這是一個新的資源ID,若是不存在,aapt工具將在R.java類中建立一個新的資源整數。例如:

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

nameTextbox名稱如今是附加到該元素的資源ID。而後,您能夠參考在Java中與ID關聯的TextView:

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

這段代碼返回TextView對象。

可是,若是您已經定義了一個ID資源(而且它尚未被使用),那麼您能夠經過在android: ID值中排除加號來將該ID應用於視圖元素。

Value for android:layout_height and android:layout_width連接

高度和寬度值能夠用Android支持的任意尺寸單位(px、dp、sp、pt、in、mm)表示,也能夠用如下關鍵詞表示:

Value Description
match_parent

Sets the dimension to match that of the parent element. Added in API Level 8 to deprecate fill_parent

設置與父元素匹配的維度。在API級別8中添加,以反對fill_parent.

wrap_content

Sets the dimension only to the size required to fit the content of this element.

僅將維度設置爲適合此元素內容所需的大小。

Custom View elements 連接

您能夠建立本身的自定義視圖和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()方法中加載一個活動的佈局:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
}

另請參閱:

    

相關文章
相關標籤/搜索