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

自訂組合控制項的兩個核心動作:
新建 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>,就能快速堆出多個外觀一致的設定條目,不必再複製整段布局。
用 AIDL 實現跨進程 Service 呼叫的完整流程
Android 四大元件中,Activity、BroadcastReceiver、ContentProvider、Service 都具備跨進程能力。ContentProv
Android 登入頁面:三機會驗證與按鈕禁用
許多 App 需要登入頁面來保護使用者資料。除了基本的帳號密碼輸入,也可以加入錯誤次數限制,提高安全性。 登入欄位設計 畫面中需要兩個 EditText,一個輸入使用者名
Android 登入功能實作:從輸入欄位到錯誤次數限制
登入畫面是許多應用的第一道門。使用者要登入社群平台、雲端硬碟、內部系統,都得先通過這關。本章拆解它的三個組成部分:輸入介面、帳密比對,以及防止暴力嘗試的次數鎖。 介面:
微信 SDK 調用總是失敗?別忽略 debug 與 release 簽名差異
接入微信 API 時卡了很久,四處找教學、翻官方文檔,文檔寫得相當含糊,只說「三步走」。結果問題根本不在那三步本身,而在一個幾乎沒人提的細節:簽名不一致。 官方說的三步