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:layoutwidth和android: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" />
參考資料