Android中include標籤的使用及注意事項,androidinclude

來源:互聯網
上載者:User

Android中include標籤的使用及注意事項,androidinclude
前言

  include標籤可以實現在一個layout中引用另一個layout的布局,這通常適合於介面布局複雜、不同介面有共用布局的APP中,比如一個APP的頂部布局、側邊欄布局、底部Tab欄布局、ListView和GridView每一項的布局等,將這些同一個APP中有多個介面用到的布局抽取出來再通過include標籤引用,既可以降低layout的複雜度,又可以做到布局重用(布局有改動時只需要修改一個地方就可以了)。

使用方法

  include標籤的使用很簡單,只需要在布局檔案中需要引用其它布局的地方,使用layout="@layout/child_layout"就可以了:

<include layout="@layout/titlebar" />

  比如,includevoicectrlbarlayout.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="61dp"    android:orientation="horizontal"    android:layout_alignParentBottom="true">    <!-- 播放、暫停 -->    <Button        android:id="@+id/voiceBtnId"        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="100"        android:background="@drawable/voice_btn_selector"        android:gravity="center"        android:onClick="onClick"        android:text="@string/voice_stop"        android:textColor="@android:color/white"        android:textSize="24sp" />    <!-- 分割線 -->    <View        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="1"        android:background="@android:color/black" />    <!-- 聽錄音 -->    <Button        android:id="@+id/listenBtnId"        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="100"        android:background="@drawable/listen_btn_selector"        android:gravity="center"        android:onClick="onClick"        android:text="@string/voice_listen"        android:textColor="@android:color/white"        android:textSize="24sp" />    <!-- 分割線 -->    <View        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="1"        android:background="@android:color/black" />    <!-- 下一個 -->    <Button        android:id="@+id/nextBtnId"        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="100"        android:background="@drawable/next_btn_selector"        android:gravity="center"        android:onClick="onClick"        android:text="@string/voice_next"        android:textColor="@android:color/white"        android:textSize="24sp" /></LinearLayout>

  在需要使用該共用布局的地方作如下調用即可:

<RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawable/background_bottom_layer">    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_margin="10dp"        android:background="@drawable/background_top_layer">        <include layout="@layout/include_voice_text_bar_layout" />        <include layout="@layout/include_voice_ctrl_bar_layout" />    </RelativeLayout></RelativeLayout>

注意事項
  • include和其它組件標籤(RelativeLayout、LinearLayout、TextView等)一樣,都可以使用layout屬性來設定布局檔案的寬高和位置,但需要注意的是:必須要複寫android:layoutwidthandroid:layoutheight屬性才能使用其它屬性(比如:android:layoutgrivity、android:layoutalign...、android:id等),這樣可以避免include引用layout中的子組件屬性影響到include的布局效果:

    <RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawable/background_bottom_layer">    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_margin="10dp"        android:background="@drawable/background_top_layer">        <include layout="@layout/include_voice_text_bar_layout" />        <include            android:layout_width="match_parent"            android:layout_height="61dp"            android:layout_alignParentBottom="true"            layout="@layout/include_voice_ctrl_bar_layout"            />    </RelativeLayout></RelativeLayout>

  • 建議將給include標籤調用布局設定寬高、位置、ID等工作放在調用布局的根標籤中,這樣可以避免給include標籤設定屬性不當造成的各種問題(之前遇到過給include標籤設定android:id屬性後,程式執行個體化子布局中組件失敗的現象):

  應該這樣:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/bottomBarLayoutId"    android:layout_width="match_parent"    android:layout_height="61dp"    android:orientation="horizontal"    android:layout_alignParentBottom="true">    。。。</LinearLayout>

<include layout="@layout/include_voice_ctrl_bar_layout" />

        而不是這樣:

<?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="horizontal">    。。。</LinearLayout>

<include    android:id="@+id/bottomBarLayoutId"    android:layout_width="match_parent"    android:layout_height="61dp"    android:layout_alignParentBottom="true"    layout="@layout/include_voice_ctrl_bar_layout"    />

參考資料
  • Does Android XML Layout's 'include' Tag Really Work?

  • Re-using Layouts with

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.