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

自訂 View 與自屬性命名空間完整攻略

編輯:進階開發

自訂 View 大家都不陌生,但「怎麼讓自訂 View 在 XML 裡吃屬性」這件事,很多人卡在命名空間上。這篇把它一次講清楚。

兩種宣告方式

如果是整個遊戲畫面或全窗介面,直接在 onCreate 裡 setContentView(myView) 就好。若當成一個可重複使用的控制項,就該在版面 XML 中宣告:

<cn.com.android123.CwjView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

也可以透過父類別的 class 屬性指定:

<View class="cn.com.android123.CwjView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

上面用到的 layout_width、layout_height 來自 android 命名空間。但自訂 View 若要做的事更多,就需要自己的屬性:

<cn.com.android123.CwjView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    cwj:age="22"
    cwj:university="sjtu"
    cwj:city="shanghai" />

命名空間怎麼寫

標準 XML 需要 xmlns 宣告。系統自帶的是:

xmlns:android="https://schemas.android.com/apk/res/android"

而我們自訂的 cwj 命名空間,兩種寫法都可行:

xmlns:cwj="https://schemas.android.com/apk/res/cn.com.android123.cwjView"
xmlns:cwj="https://schemas.android.com/apk/res/android"

差別在於:前者綁定到自訂 View 的完整類別名稱,後者沿用 android 命名空間的 URI。兩者 Android 都能解析,但建議使用前者,語意較清楚。

宣告屬性:declare-styleable

在 res/values/ 下新增一個檔案(例如 cwj_attr.xml,記得用 UTF-8 編碼):

<?xml version="1.0" encoding="utf-8" ?>
<resources>
    <declare-styleable name="CwjView">
        <attr name="age"        format="integer" />
        <attr name="city"       format="string" />
        <attr name="university" format="string" />
    </declare-styleable>
</resources>

declare-styleable 的 name 對應自訂 View 的類別名稱,這個名字會決定 R 檔裡產生的常數前綴。

format 有哪些值

  • integer:整數,例如 ProgressBar 的進度值
  • float:浮點,例如 RatingBar 的 3.5 顆星
  • boolean:布林,例如 ToggleButton 的勾選狀態
  • string:字串,例如 TextView 的 text
  • color:顏色,可辨識 #FF0000 這類寫法
  • dimension:尺寸,如 23dip、15px、18sp
  • reference:資源引用,如 @+id/cwj、@drawable/xxx

多種類型可以用位元或運算子合併,讓同一個屬性既能寫實際值也能寫資源引用:

<attr name="red" format="color|reference" />

在程式碼中讀取

最正規的方式是 obtainStyledAttributes:

public CwjView(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CwjView);

    mAge        = a.getInteger(R.styleable.CwjView_age, 22);
    mCity       = a.getString(R.styleable.CwjView_city, "shanghai");
    mUniversity = a.getString(R.styleable.CwjView_university, "sjtu");

    a.recycle();   // 別忘了回收,TypedArray 持有原生資源
}

getXxx() 的最後一個參數是預設值——XML 裡沒寫就採用它。這個重載版本(帶 AttributeSet 的建構子)只有在透過 XML 膨脹時才會被呼叫,用 new 直接建立的情況不會走到,設計 API 時要留意。

另一條路:getAttributeResourceValue

若你想直接拿到資源 ID(例如 @drawable/xxx 的整數值),可以用:

public CwjView(Context context, AttributeSet attrs) {
    super(context, attrs);
    int resId = attrs.getAttributeResourceValue(
            "cn.com.android123.CwjView", "age", 100);
    String city = attrs.getAttributeValue(
            "cn.com.android123.CwjView", "city");
}

最後一個參數同樣是找不到時的預設值。這種寫法更直接,但少了型別轉換的保護,我個人在多數情況下還是偏好 obtainStyledAttributes——它會幫你處理 color、dimension 這類需要換算的型別,也比較容易擴充。

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