編輯:Android 資訊
做 Android 開發時,幾乎每個人都遇過這樣的場景:好幾個頁面的頂部標題欄、底部功能列長得一模一樣,卻被反覆複製到各個佈局檔案裡。結果就是 XML 動輒幾百行,改一個按鈕顏色要翻遍半個專案,可讀性和維護性都很差。
Android 為這種情況準備了現成的答案——<include /> 標籤。把重複的佈局片段抽成獨立檔案,需要時引用進來,標題欄之類的公共模組就只需要維護一份。
以下是一個典型的頂部標題欄,含標題文字與一個返回按鈕:
app_title.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/titleLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/bt"
xmlns:android="https://schemas.android.com/apk/res/android">
<TextView android:id="@+id/title"
android:text="@string/login"
android:textSize="20px"
android:textColor="@color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<Button android:id="@+id/refresh"
android:text="返回"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:focusable="false"
android:textColor="@color/white"
android:background="@drawable/okbutton"
android:layout_marginRight="3px"/>
</RelativeLayout>
app_tradelogin.xml(主體內容,放在 ScrollView 裡,上下分別留出標題欄與底部列的空間)
<?xml version="1.0" encoding="utf-8"?>
<!-- android:layout_below 與 android:layout_above 必須設定,
否則介面會錯位;不設 layout_above 還會失去滑動效果 -->
<ScrollView android:layout_below="@+id/titleLayout"
android:layout_above="@+id/appbottom"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
xmlns:android="https://schemas.android.com/apk/res/android">
<LinearLayout android:id="@+id/theWholeLinearLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10px"
android:paddingTop="6px"
android:paddingRight="10px">
<TableLayout android:id="@+id/widget43"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:stretchColumns="1">
<TableRow>
<TextView android:id="@+id/traderName" android:text="@string/traderName" />
<Spinner android:id="@+id/traderNameS"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<TextView android:id="@+id/departName" android:text="@string/departName" />
<Spinner android:id="@+id/departNameS"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<TextView android:id="@+id/strComponyName" android:text="@string/accType" />
<Spinner android:id="@+id/accTyte"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<TextView android:id="@+id/strUserAcc" android:text="@string/userAcc" />
<EditText android:id="@+id/accEditText"
android:numeric="decimal" android:text="" />
</TableRow>
<TableRow>
<TextView android:id="@+id/userPwd" android:text="@string/userPWD" />
<EditText android:id="@+id/userPwdET"
android:password="true" android:text="" />
</TableRow>
<TableRow>
<TextView android:id="@+id/commPwd" android:text="@string/commPWD" />
<EditText android:id="@+id/commPwdET"
android:password="true" android:text="" />
</TableRow>
</TableLayout>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="1">
<CheckBox android:id="@+id/recordAcc"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:text="@string/saveUserAcc"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="1">
<CheckBox android:id="@+id/hideAcc"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:text="@string/ycUserAcc"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<LinearLayout android:layout_width="220px"
android:layout_height="wrap_content"
android:orientation="horizontal" android:gravity="center">
<Button android:id="@+id/confirmexch"
android:text="@string/login"
android:layout_width="wrap_content" android:layout_height="50px"
android:textSize="22dp" android:layout_weight="1"
android:gravity="center" android:focusable="false"
android:textColor="@color/white" android:background="@drawable/buttonl"/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:gravity="center">
<ImageView android:id="@+id/myImage"
android:layout_width="wrap_content" android:layout_height="50px"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
app_bottom.xml(底部功能列)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/appbottom"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
xmlns:android="https://schemas.android.com/apk/res/android">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="@drawable/light">
<ImageView android:id="@+id/about"
android:layout_width="30px" android:layout_height="wrap_content"
android:src="@drawable/ttt" android:layout_alignParentLeft="true"/>
<TextView android:id="@+id/light"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textColor="@color/black"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="@drawable/mainmenu">
<Button android:id="@+id/quotButton" ... />
<Button android:id="@+id/entrustButton" ... />
<Button android:id="@+id/queryButton" ... />
<Button android:id="@+id/yinZhengButton" ... />
<Button android:id="@+id/recordButton" ... />
<Button android:id="@+id/logoutButton" ... />
<!-- 各按鈕均為 layout_weight="1"、layout_width="52px"、
background="@drawable/button1" -->
</LinearLayout>
</LinearLayout>
tradelogin_portrait.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="https://schemas.android.com/apk/res/android">
<include android:id="@id/titleLayout" layout="@layout/app_title" />
<include layout="@layout/app_tradelogin"/>
<include layout="@layout/app_bottom"/>
</RelativeLayout>
1. include 上寫 RelativeLayout 的排列屬性沒有效果。像 android:layout_alignParentBottom 這類由父容器解釋的參數,寫在 <include> 標籤上是不會生效的,必須寫在被打開的佈局根節點裡。
2. 在 include 上指定 id 會蓋掉原佈局的 id。一旦寫了 android:id,被引用佈局根節點原本的 id 就失效了。原因可以理解為 include 相當於在外層多包了一層,或者說是覆寫了原屬性。實務上要靠這個 id 來定位標題欄時,記得 Java 端取到的 View 是 include 這一層。
3. 加載順序帶來的意外好處。如果所有佈局都塞在一個 XML 裡,系統按順序解析,前面的節點引用不到後面節點的 id。改用 include 之後,一個 include 反而可以引用後面 include 中的 id——上面 app_tradelogin.xml 用 layout_below="@+id/titleLayout" 提前引用標題欄,就是靠這個特性工作的。
最終效果:

小結一下:include 的價值不只是少寫幾行 XML,而是把「介面骨架」和「介面內容」拆開,讓公共元件真正只存在一份。項目越大,這種拆分帶來的收益越明顯。
Android UI 元件系列:Tab Layout 選項卡版面配置
要建立一個選項卡的 UI,你需要使用 TabHost 與 TabWidget。TabHost 必須是版面配置檔的根節點,它包含了用來顯示選項卡的 TabWidget,以及
Android 單元測試研究與實踐
Android 單元測試介紹身處高速迭代開發中的 Android 項目,往往需要比黑盒測試更可靠的質量保障,這正是單元測試的用武之地。單元測試周期性地對項目進行函數級別的
在 Android 應用中整合 Google 登入
整合 Google 登入 Google 登入讓使用者不必另外註冊帳號,也能快速進入 App。實作重點包括建立 Google API 設定、下載 google-servic
理解 Android Binder 機制(一):從驅動層看起
Binder 的實作相當複雜,想徹底弄懂並不容易。它橫跨好幾個層次,每一層都有各自的模組與機制要理解。這系列預計分三篇,本文是第一篇:先建立整體架構的骨架,再把主要篇幅留