Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

解析 Android 中 include 標籤的使用方式

編輯:關於 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>

二、主布局用 include 組裝

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>

三、include 的幾個限制

實測時有幾點值得記下來:

  • include 標籤上設定 RelativeLayout 專屬屬性(如 layout_alignParentBottom)不會生效,因為它的父容器不一定是 RelativeLayout。
  • 若在 include 上指定 android:id,被引用布局內原本的 id 會被覆蓋掉。
  • include 支援 android:layout_* 這類通用布局屬性的覆寫,可視情況調整被引入片段的尺寸與權重。

另一個實務上的好處是載入順序:所有布局寫在一個檔案時,前面的片段無法引用後面才定義的 id;改用 include 之後,這個限制自然解除。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved