編輯:關於 Android 開發
自從開始深入研究 Android 自訂控制項之後,最繞不過去的一關就是「屬性」:怎麼定義自己的 XML 屬性、怎麼在版面檔裡引用、又怎麼在 Java 程式碼裡把它讀出來。這篇文章把整條流程走一遍,包含 attrs.xml 的編寫、declare-styleable 的作用,以及 TypedArray 的資源映射機制。
自訂屬性一律定義在 res/values/attrs.xml(或任何 values 下的資源檔)中:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 先逐一宣告屬性名稱與資料型別 -->
<attr name="titleText" format="string" />
<attr name="titleTextColor" format="color" />
<attr name="titleTextSize" format="dimension" />
<!-- 再把這些屬性歸類到一個 styleable 集合中 -->
<declare-styleable name="AuthCodeView">
<attr name="titleText" />
<attr name="titleTextColor" />
<attr name="titleTextSize" />
</declare-styleable>
</resources>
這裡有兩個關鍵標籤:
「<attr>」用來定義屬性的名字與型別,常見的 format 有 string、color、dimension、boolean、integer、reference、enum、flag 等。
「<declare-styleable>」則是一個集合,把相關的屬性打包在一起。這裡集合名叫 AuthCodeView,稍後在 Java 程式碼中會直接對應到 R.styleable.AuthCodeView。
先對照兩張示意圖,大半的觀念就清楚了。
a、自訂屬性的命名空間引用:

b、仔細比對圖中的說明與 a、b 兩圖的差異,就能理解屬名前綴是怎麼產生的:

以下附上部分 XML 程式碼:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
xmlns:authcodeview="https://schemas.android.com/apk/res/com.example.authcodeview"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.example.authcodeview.view.AuthCodeView
android:id="@+id/AuthCodeView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
authcodeview:titleText="3712"
authcodeview:titleTextColor="#00ffff"
authcodeview:titleTextSize="40sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="點擊驗證碼,換一張" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="輸入驗證碼" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="驗證" />
</LinearLayout>
請特別注意根節點上的兩行命名空間宣告。
xmlns:android="https://schemas.android.com/apk/res/android" 是系統屬性的引用,也就是我們平時寫的 android:layout_width 的來源。
xmlns:authcodeview="https://schemas.android.com/apk/res/com.example.authcodeview" 則是自訂屬性的引用,命名規則為:
xmlns:名稱 = "https://schemas.android.com/apk/res/ + 應用的包名"
宣告好之後,使用方式就是「前綴 + 屬性名」:
authcodeview:titleText="3712"
authcodeview:titleTextColor="#00ffff"
authcodeview:titleTextSize="40sp"
順便附上系統 attrs 檔在原始碼中的位置:

XML 裡設定的值,要透過 TypedArray 才能取回來:
/**
* 取得我們自訂的樣式屬性
*
* @param context 上下文
* @param attrs XML 中宣告的屬性集合
* @param defStyle 預設樣式
*/
public AuthCodeView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// 取得 declare-styleable 中名為 AuthCodeView 的屬性集合
TypedArray a = context.getTheme()
.obtainStyledAttributes(attrs, R.styleable.AuthCodeView, defStyle, 0);
// 查詢這個集合裡有幾個屬性
int n = a.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = a.getIndex(i);
switch (attr) {
case R.styleable.AuthCodeView_titleText:
mTitleText = a.getString(attr);
break;
case R.styleable.AuthCodeView_titleTextColor:
// 預設顏色設為黑色
mTitleTextColor = a.getColor(attr, Color.BLACK);
break;
case R.styleable.AuthCodeView_titleTextSize:
// 預設 16sp,TypedValue 也能把 sp 換算成 px
mTitleTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, 16,
getResources().getDisplayMetrics()));
break;
}
}
a.recycle();
}
TypedArray 扮演的是「資源映射」的角色:R.styleable.AuthCodeView 對應到 declare-styleable 集合,而 R.styleable.AuthCodeView_titleText 則是「集合名 + 底線 + 屬性名」的命名慣例。這樣一來,XML 上設定的值就完整映射進 Java 物件裡了。
最後別忘了 a.recycle(),這是 Android 資源管理的基本禮儀。若想確認生成的 ID 是否符合預期,可以打開自動產生的 R.java 檔案對照看看。
以上是自訂屬性的基本流程。下一篇會把這些屬性應用到實際的隨機驗證碼自訂 View 上。
Android 定位實戰:基於百度地圖實現定位功能
Android 定位實戰:基於百度地圖實現定位功能 一、問題描述 LBS 位置服務是 Android 應用中越來越重要的功能,應用也越來越廣泛。下面我們循序漸進地學習並實
Android 自動完成輸入框 AutoCompleteTextView 使用教學
輸入前幾個字就自動補全 AutoCompleteTextView 能在使用者輸入單詞或句子的前幾個字時,自動彈出匹配的候選內容,大幅減少輸入。效果如下: 一、MainA
FloatingActionButton 進階用法:一個主按鈕展開多個子按鈕(開源元件實作)
今天要介紹的是 FloatingActionButton 的進階玩法——透過 FloatingActionsMenu 容器,讓一個懸浮按鈕點擊後展開一整組子按鈕,是 Ma
Android 仿即刻首頁垂直滾動圖:自訂 ViewGroup 動畫
Android 仿即刻首頁垂直滾動圖:自訂 ViewGroup 動畫 首頁頂部垂直輪播圖片是常見的視覺效果。本文以仿即刻 App 的垂直滾動圖為例,說明如何透過自訂 Vi