Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 手機衛士(十四):自定義組合控件構建布局結構

編輯:Android 開發實例

設置中心的各個條目長得幾乎一模一樣,如果每個都手寫一遍布局,程式碼會迅速膨脹。這時候自訂組合控制項就是正解。

自訂組合控制項的兩個核心動作:

  1. 把已寫好的布局抽取出來,交由一個類別統一管理,之後任何地方要複用這段結構,只要建立對應的物件即可。
  2. 把組合控制項的布局獨立到一個 XML 檔中。

一、抽出布局檔

新建 setting_item_view.xml,把前一篇的條目布局原封不動搬進來:

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp" >

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="自動更新設置"
            android:textColor="#000"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tv_des"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/tv_title"
            android:text="自動更新已關閉"
            android:textColor="#000"
            android:textSize="18sp" />

        <CheckBox
            android:id="@+id/cb_box"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_below="@id/tv_des"
            android:background="#000" />
    </RelativeLayout>

</RelativeLayout>

二、建立組合控制項類別

透過一個獨立類別 SettingItemView.java 來載入這段布局:

package com.wuyudong.mobilesafe.view;

import com.wuyudong.mobilesafe.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SettingItemView extends RelativeLayout {

    private TextView tv_des;
    private CheckBox cb_box;

    public SettingItemView(Context context) {
        this(context, null);
    }

    public SettingItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        // xml --> view:把設置界面的條目轉換成 view 物件
        View.inflate(context, R.layout.setting_item_view, this);

        // 等同於以下兩行代碼
        /*
         * View view = View.inflate(context, R.layout.setting_item_view, null);
         * this.addView(view);
         */

        tv_des = (TextView) findViewById(R.id.tv_des);
        cb_box = (CheckBox) findViewById(R.id.cb_box);
    }
}

這裡的三個建構子不是多餘的。(Context) 版本供程式碼中動態 new 出來時使用,(Context, AttributeSet) 版本是 LayoutInflater 從 XML 反序列化時會呼叫的,(Context, AttributeSet, int) 則是帶預設樣式的完整版本。後兩者形成建構子鏈,是 Android 自訂 View 的標準寫法。

另外要特別注意 View.inflate(context, layout, this) 的第三個參數——傳入 this 而非 null,inflate 出來的子 View 才會直接掛到目前這個 ViewGroup 上,並讓 findViewById 能正確找到子控制項。若傳 null,就得自己 addView,否則畫面會一片空白。

三、在布局檔中使用

之後要複用時,只要幾行 XML 就能搞定:

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

    <TextView
        style="@style/TitleStyle"
        android:text="設置中心" />

    <!-- 原本那一大段 RelativeLayout 已被下方組合控制項取代
    <RelativeLayout ... >
        ...
    </RelativeLayout>
    -->

    <com.wuyudong.mobilesafe.view.SettingItemView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </com.wuyudong.mobilesafe.view.SettingItemView>

</LinearLayout>

執行專案後的效果:

接下來只要在 XML 中重複貼上 <com.wuyudong.mobilesafe.view.SettingItemView>,就能快速堆出多個外觀一致的設定條目,不必再複製整段布局。

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