編輯:關於 Android 開發
由於設置中心中的各個條目布局非常相似,逐一編寫既費時又難以維護。本文將這些相似布局抽取為自定義組合控件 SettingItemView,從而大幅簡化實現。

1. 將已編寫好的布局抽取到一個類中進行管理,下次需要時直接使用組合控件對應的對象;
2. 將組合控件的布局單獨抽取到一個 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>
新建一個類去加載這段布局文件,並在其中獲取各子控件:
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, null, 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);
// 獲取組合控件中的標題描述
TextView tv_title = (TextView) findViewById(R.id.tv_title);
tv_des = (TextView) findViewById(R.id.tv_des);
cb_box = (CheckBox) findViewById(R.id.cb_box);
}
}
封裝完成後,只需幾行代碼即可完成布局調用:
<?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="設置中心" /> <com.wuyudong.mobilesafe.view.SettingItemView android:layout_width="match_parent" android:layout_height="wrap_content" > </com.wuyudong.mobilesafe.view.SettingItemView> </LinearLayout>
運行項目後,效果如下:

Android 提高 21 篇之七:SAX 解析 XML 與 XmlSerializer 生成 XML
XML 是 Android 開發中常見的資料交換格式。解析方式有 DOM 與 SAX 兩種。DOM 會先把整份文件載入記憶體,適合需要隨機存取節點的情境,但較耗記憶體;S
DB2 性能調優路線圖——LOG 機制入門詳解
本文在整理報表時,對 DB2 STAT LOG ACTIVITY 部分做了補充,主要介紹 DB2 LOG 的作用、overhead、涉及的部件及部件間的工作機制,以及如何
Android 開發環境配置:Eclipse 關聯源碼與源碼/JAR 查看工具
想更深入理解 Android 底層運作,關聯系統源碼、選對查看工具是必備技能。本文介紹三項實用配置。 1、Eclipse 關聯源碼 步驟一:自行下載對應版本的源碼,本文以
Android Studio 每日一帖:重構技巧之「提煉為常量」
《Android Studio 每日一帖》11. 重構之提煉為常量 「提煉為常量」(Extract Constant)能把程式碼中重複出現的魔法值收攏成一個具名的靜態常量