編輯:關於 Android 程式設計
同一個專案裡常會出現重複的布局片段,例如標題列、底部選單。若全部寫在同一個 xml 裡,檔案會越長越難讀,改一處還得同步改多處。這時可以把它們抽成獨立檔案,用 <include /> 標籤 reuse。
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:text="@string/login" android:id="@+id/title"
android:textSize="20px" android:textColor="@color/white"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:layout_height="wrap_content"/>
<Button android:text="返回" android:id="@+id/refresh"
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_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" android:layout_weight="1"
android:layout_width="52px" android:layout_height="wrap_content"
android:textColor="@color/white" android:text=""
android:background="@drawable/button1"/>
<!-- 其餘按鈕結構相同,此處省略 -->
</LinearLayout>
</LinearLayout>
app_tradelogin.xml 是內容區,放在 ScrollView 中,並透過 layout_below 與 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">
<!-- 表格內容:交易員、部門、帳戶類型、帳號、密碼等列 -->
</LinearLayout>
</ScrollView>
這裡的 layout_below 與 layout_above 是必須的:少了它們,界面會錯位,也拿不到滑動效果。
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>
實測時有幾點值得記下來:
layout_alignParentBottom)不會生效,因為它的父容器不一定是 RelativeLayout。android:id,被引用布局內原本的 id 會被覆蓋掉。android:layout_* 這類通用布局屬性的覆寫,可視情況調整被引入片段的尺寸與權重。另一個實務上的好處是載入順序:所有布局寫在一個檔案時,前面的片段無法引用後面才定義的 id;改用 include 之後,這個限制自然解除。

手機就能做圖片教程 兩種簡便方法分享
不少人習慣把截圖傳到電腦,用PS標註加工後再發到社交平台,來回折騰其實並不省事。事實上Android手機完全可以勝任這件事,而且並不需要專業的修圖軟件。 這裡分享兩種在手
Android 四大數據存儲方式詳解:SharedPreferences、文件、SQLite、ContentProvider
本文大部分內容整理自《Android 開發全程實錄》,推薦有興趣的讀者閱讀原書以獲得更系統的知識。 一、SharedPreferences SharedPrefer
Android 學習筆記:AsyncTask 實現檔案下載任務
(1)本範例主要示範以異步任務下載圖片。(2)佈局代碼如下:一個 ImageView 用於存放下載的圖片,一個 Button 用於開始異步任務下載。 (3)控制類:使
Android 開發者值得閱讀的經典書籍推薦
Android 開發需要扎實的 Java 基礎、平台知識與架構思維。以下整理幾本常見且值得閱讀的書籍,可依自身程度選擇。 Java 基礎與物件導向 《Java 編程思想》