android自學之旅——layout資源檔

來源:互聯網
上載者:User

標籤:

本文只是我在看官方協助文檔的翻譯和理解,以方便自己日後查看。

什麼是layout資源檔?layout一個定義了Activity或者組件的布局的xml檔案。

它一般長這樣:

<?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" | "fill_parent" | "wrap_content"]    android:layout_width=["dimension" | "fill_parent" | "wrap_content"]    [ViewGroup-specific attributes] >    <View        android:id="@[+][package:]id/resource_name"        android:layout_height=["dimension" | "fill_parent" | "wrap_content"]        android:layout_width=["dimension" | "fill_parent" | "wrap_content"]        [View-specific attributes] >        <requestFocus/>    </View>    <ViewGroup >        <View />    </ViewGroup>    <include layout="@layout/layout_resource"/></ViewGroup>

注意:根節點可以是一個ViewGroup、View或者merge節點,但是只能有一個有一個根節點,並且它必須有xml:android屬性來定義命名空間。

節點介紹:

  <ViewGroup>

    它是一個可以放很多view的容器。ViewGroup有很多種,主要包含LinearLayout、RelativeLayout和FrameLayout。

    屬性:

    android:id

      它是一個節點唯一標識名,你可以在應用程式中通過該標識名來應用一個ViewGroup。

    android:layout_height

      ViewGroup的高度,值可以是一個尺寸值,也可以是“fill_parent”或者“wrap_content”,它是必須的。

    android:layout_width

      ViewGroup的寬度,值可以是一個尺寸值,也可以是“fill_parent”或者“wrap_content”,它是必須的。

    以上只是一個ViewGroup最基本的屬性,不同的ViewGroup還有很多各自屬性。

  <view>

    一個UI組件比如:TextView、Button、CheckBox。

    通用的屬性同ViewGroup,還有很多屬於不同View各自的屬性。

  <requestFocus>

    任何一個View節點都可以包含改屬性,該屬性使其父元素在初始化的時候擷取焦點,但是每個layout中只允許一個節點擁有該屬性。

  <include>

    該標籤可以通過用的方式將其他定義好的layout檔案加入到當前layout。

    屬性:

    layout:

      layout資源的引用,必須的。

    android:id

      資源ID,重寫當前的包含的layout的ID提供給根視圖。

    android:layout_height

      重寫當前包含的layout的高度提供給根視圖,只有同時定義了android:layout_width才有效。

    android:layout_width

      重寫當前包含的layout的寬度提供給根視圖,只有同時定義了android:layout_height才有效。

    你也可以在include中重寫根視圖支援的include layout中的其他的屬性。

    另一個包含一個layout方式是使用ViewStub。它是一個輕量級的視圖不佔用任何layout控制直到你明確的inflate它,可以通過android:layout來使用它。

  <merge>

    當前你想使用的ViewGroup已經包含的在layout中,並且你想通過include加入到當前ViewGroup中,可以使用當前元素作為根節點,將include layout中的

    ViewGroup與當前ViewGroup合并。

  Value for android:id

  定義改屬性的值一般都是通過“@+id/name”這種形式,“+”表示這個是一個新的ID,如果這個“name”不存在,那麼aapt工具將會建立一個新的resource integer在R.java類

  中。例如:

  

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

  這個“nameTextbox”現在就可以作為當前TextView的資源ID使用。你可以通過資源ID以下方式來引用TextView在java代碼中:

findViewById(R.id.nameTextbox);

  這段代碼將返回TextView的對象。

  Value for android:layout_heigth and android:layout_width

  heigth和width值可以是android支援的尺寸單位的值(px、sp、sp、pt、in、nm)或者是以下內容:

Value Descrption
match_parent 設定控制項的尺寸和父級元素一致,API8中添加。
fill_parent 設定當前控制項的尺寸與父級元素一致,在API8以上過時。
wrap_content 設定控制項尺寸為包含控制項中內容(圖片或文字)

  執行個體:

  res/layout/main_activity.xml中的layout布局檔案:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:layout_width="fill_parent"               android:layout_height="fill_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>

在Acitivity的onCreate載入該布局:

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

android自學之旅——layout資源檔

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.