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

詳解 Android 自定義控件屬性 TypedArray 以及 attrs

編輯:關於 Android 程式設計

近來在研究 Android 自定義控件屬性,重點接觸了 TypedArray 與 attrs。本文將系統性地梳理它們的用法,後續還會有實際應用篇章。

1、attrs 檔案編寫

 <?xml version="1.0" encoding="utf-8"?> <resources> <attr name="titleText" format="string" /> <attr name="titleTextColor" format="color" /> <attr name="titleTextSize" format="dimension" /> <declare-styleable name="AuthCodeView"> <attr name="titleText" /> <attr name="titleTextColor" /> <attr name="titleTextSize" /> </declare-styleable> </resources> 

上面的程式碼定義了三個屬性。其中 attr 標籤用來定義屬性的名字與格式,而 declare-styleable 則是一個屬性組,這裡取名為 AuthCodeView,後續在 class 中就會用到。

2、在 XML 中如何引用與使用

先看兩張示意圖,就能理解大半:

對比這兩張圖,你就能明白屬性名改變後該如何引用。以下附上部分 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" > <requestFocus /> </EditText> </LinearLayout> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="驗證" /> </LinearLayout> 

重點在於頭部:xmlns:android="..." 是引用系統屬性的命名空間,而 xmlns:authcodeview="https://schemas.android.com/apk/res/com.example.authcodeview" 則是引用自定義屬性。命名規則為:xmlns:+名稱 = "https://schemas.android.com/apk/res/ + 應用的包名"。使用時寫成 authcodeview:titleText、authcodeview:titleTextColor、authcodeview:titleTextSize 即可。

3、在自定義控件的 class 中如何引用

關鍵在於透過 TypedArray 取得屬性:

 /** * 獲得我自定義的樣式屬性 * * @param context * @param attrs * @param defStyle */ public AuthCodeView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); /** * 獲得我們所定義的自定義樣式屬性 */ TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.AuthCodeView, defStyle, 0); //獲取在attr文件下,名字為AuthCodeView的declare-styleable屬性有幾個 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,TypeValue也可以把sp轉化為px mTitleTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics())); break; } } a.recycle(); } 

TypedArray 本質上扮演的是資源映射的角色。像 R.styleable.AuthCodeView 對應整個屬性組,而 R.styleable.AuthCodeView_titleText 則是「組名+下橫線+屬性名」的組合。透過這樣的對應,就能把 XML 中設定的自定義屬性值映射到 class 裡,取值時格外方便。若需要繼續深入,可參閱續篇《Android 自定義控件深入學習 Android 生成隨機驗證碼》。

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