前言
??<include>標簽可以實現在一個layout中引用另一個layout的布局,這通常適合于界面布局復雜、不同界面有共用布局的APP中,比如一個APP的頂部布局、側邊欄布局、底部Tab欄布局、ListView和GridView每一項的布局等,將這些同一個APP中有多個界面用到的布局抽取出來再通過<include>標簽引用,既可以降低layout的復雜度,又可以做到布局重用(布局有改動時只需要修改一個地方就可以了)。
使用方法
??<include>標簽的使用很簡單,只需要在布局文件中需要引用其它布局的地方,使用layout="@layout/child_layout"就可以了:
<include layout="@layout/titlebar" />
??比如,include_voice_ctrl_bar_layout.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:layout_width和android:layout_height屬性才能使用其它屬性(比如:android:layout_grivity、android:layout_align...、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"
/>