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

Android 佈局標籤 include:讓 XML 重複利用

編輯:關於 Android 程式設計

一個專案裡經常會需要用到相同的佈局設計。如果每次都複製貼上到同一個 XML 裡,程式碼冗餘、可讀性又差。解法是把共用部分抽成獨立檔案,用 <include> 標籤重複使用。

一、抽出具體的 btn.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

二、在 main.xml 中重複引用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <include
        android:id="@+id/in1"
        layout="@layout/btn" />

    <include
        android:id="@+id/in2"
        layout="@layout/btn" />

    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
</LinearLayout>

三、TestActivity

package com.hilary;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * @author  hilary
 * @Date    2011-12-8
 */
public class TestActivity extends Activity {

    private TextView tv = null;
    private LinearLayout ll = null;
    private LinearLayout ll2 = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.tv);

        // 如果一個佈局檔中包含了同一個 XML 檔案,
        // 這兩個 XML 中的控制項 Id 是一樣的。
        // 當需要操作這些控制項時,需要透過定義這兩個 View 來加以區分。
        // 如果只包含同一個 XML 檔案,就不需要這步操作。
        ll = (LinearLayout) findViewById(R.id.in1);
        ll2 = (LinearLayout) findViewById(R.id.in2);

        ll.setBackgroundColor(Color.RED);

        Button btn = (Button) ll.findViewById(R.id.btn);
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                tv.setText("My name is hilary");
            }
        });

        Button btn2 = (Button) ll2.findViewById(R.id.btn);
        btn2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                tv.setText("You select second Button!");
            }
        });
    }
}

四、這裡有個關鍵細節

用 include 重複引入同一個檔案時,被引入的檔案裡所有控制項的 ID 都會重複。上面範例中兩個 btn 的 ID 都是 R.id.btn。

正確做法是透過 include 上的 android:id 先取得外層容器(R.id.in1、R.id.in2),再用該容器的 findViewById() 去找內部的按鈕。如果直接呼叫 Activity 的 findViewById(R.id.btn),永遠只會拿到第一個,第二個按鈕的監聽器根本設不上去——這是 include 最常見的 bug。

五、include 的三個可覆寫屬性

被引入的根 View 可以透過 include 標籤覆寫部分屬性:

  • android:id:覆寫被引入根 View 的 ID,這也是區分多個實例的關鍵。
  • android:layout_*:可以覆寫寬高、邊距等 LayoutParams(但 layout_width 與 layout_height 必須同時指定才會生效,只寫一個會被忽略)。

六、為什麼不用 include 就不行

如果所有佈局程式碼都寫在一個 XML 裡,介面會顯得很冗餘、可讀性極差。而且 Android 的佈局是按順序解析的,前面的佈局不能參照其後面佈局的 ID。採用 include 之後,一個 include 中可以參照其後續 include 裡的佈局 ID 屬性,模組之間的依賴關係清楚多了。

補充:include 與 merge 的差別

如果你的目的只是「消除多餘的巢狀 LinearLayout」,那要用的是 <merge> 而不是 <include>。merge 不會產生額外一層 ViewGroup,會直接把子 View 掛到父容器上,能有效減少 view 層級、提升繪製效能。使用 merge 的前提是它必須搭配 LayoutInflater.inflate() 且 root 不為 null,透過 setContentView() 直接使用是會報錯的。

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