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

Android 佈局複用技巧:include 與 include+merge 用法實例全解析

編輯:關於 Android 開發

Android 佈局複用技巧:include 與 include+merge 用法實例全解析

在做 Android 介面開發時,經常會遇到多個頁面要重複使用同一塊佈局的情況。與其複製粘貼,不如用 <include> 標籤把子佈局引入,既整潔又好維護。本文就用兩個實戰例子,帶大家徹底搞懂 include 單獨使用,以及 include 搭配 merge 的兩種經典玩法。

一、include + LinearLayout 的使用範例

先看 Activity 的程式碼,重點在於透過 findViewById 先拿到被引入的子佈局,再從子佈局裡定位其中的按鈕,分別綁定不同的事件:

package com.AndroidIncludeLayout;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class AndroidIncludeLayout extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        View subLayout1 = (View)findViewById(R.id.main1);
        View subLayout2 = (View)findViewById(R.id.main2);
        Button myButton_main1 = (Button)subLayout1.findViewById(R.id.mybutton);
        Button myButton_main2 = (Button)subLayout2.findViewById(R.id.mybutton);
        Button startAnotherActivity = (Button)findViewById(R.id.startanotheractivity);
        startAnotherActivity.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent();
                intent.setClass(AndroidIncludeLayout.this, AnotherActivity.class);
                startActivity(intent);
            }});
        myButton_main1.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View arg0) {
                Toast.makeText(AndroidIncludeLayout.this, "Button 1 Pressed", Toast.LENGTH_LONG).show();
            }});
        myButton_main2.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View arg0) {
                Toast.makeText(AndroidIncludeLayout.this, "Button 2 Pressed", Toast.LENGTH_LONG).show();
            }});
    }
}

主佈局 main.xml 裡,用兩個 <include> 分別引入同一個子佈局,並各自指定不同的 id,這樣才能對它們分別操作:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <include android:id="@+id/main1" layout="@layout/sublayout" />
    <include android:id="@+id/main2" layout="@layout/sublayout" />
    <Button
        android:id="@+id/startanotheractivity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" Start Another Activity " />
</LinearLayout>

被引入的子佈局 sublayout.xml,這裡是一個帶背景色的線性佈局,裡面放了一個文字和一個按鈕:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#505050" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="SubLayout" />
    <Button
        android:id="@+id/mybutton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=" A Button " />
</LinearLayout>

二、include + merge 的使用範例

這種寫法和上面的 include 有點不太一樣,最大的特點是:使用 <merge> 作為子佈局的根節點時,可以直接一步調用 merge 裡面的子元件 id,不必先經過 include 再去找。

Activity 程式碼如下,注意它直接 findViewById(R.id.temperature_light) 就拿到了被 merge 包裹的自訂元件:

package com.hysmarthotel.roomcontrol;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

//燈光控制
public class LightActivity extends Activity {
    public static Temperature temp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.light);
        temp = (Temperature)findViewById(R.id.temperature_light);
        //特殊的地方是,可以直接一步調用 merge 中的 id,不用通過 include 再去調用
        init();
    }

    private void init() {
        temp.setTemperature(MainActivity.mTemp + "");
    }
}

主佈局 light.xml 採用絕對定位佈局,同樣用 include 引入 time_temp:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg1" >
    <TextView
        android:id="@+id/lightctrl"
        android:layout_x="91.5px"
        android:layout_y="93.0px"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:textSize="42.0px"
        android:textColor="#fff3e3d1"
        android:text="@string/light_ctrl"
        android:drawableLeft="@drawable/ic_light_t"
        android:drawablePadding="6px"/>
    <include android:id="@+id/include1" layout="@layout/time_temp"/>
</AbsoluteLayout>

而 time_temp.xml 的關鍵,就是以 <merge> 作為根節點。merge 的作用是:引入時不額外多包一層父容器,直接把裡面的元件「灌」進父佈局中,因此元件 id 能被直接使用:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:hyhotel="https://schemas.android.com/apk/res/com.hysmarthotel.roomcontrol" >
    <com.hysmarthotel.view.Temperature
        android:id="@+id/temperature_light"
        android:layout_x="1545.0px"
        android:layout_y="109.5px"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22.5px"
        android:textColor="#fff3e3d1"
        hyhotel:prefix="@string/room_temp"
        hyhotel:unit="@string/celsius" />
</merge>

小結一下:普通 <include> 引入時會保留子佈局的根容器,需要透過 include 的 id 再往下找元件;而搭配 <merge> 時則會「打平」這一層,讓元件 id 直接暴露給父佈局使用,適合用在自訂容器或需要精簡層級的場景。

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