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

Android 自定義屬性用法詳解:attr.xml 與 TypedArray 解析機制

編輯:關於 Android 程式設計

做自定義控制項時,總會希望它能在 XML 佈局裡像系統控制項一樣被設定屬性。要達成這件事,得先學會宣告自定義屬性,並在程式碼中把它讀出來。

屬性宣告的位置與格式

自定義屬性集中宣告在 res/values/attr.xml,基本結構如下:

<resource>

<declare-styleable name="自定義屬性名稱">

<attr name="屬性名稱" format="屬性種類"/>

......

</declare-styleable>

</resource>

declare-styleable 的 name 通常取控制項的完整類名,attr 的 format 則決定這個屬性能接受哪一類值。

十種 format 逐一說明

reference —— 引用某個資源 ID

<declare-styleable name = "名稱">
    <attr name = "background" format = "reference" />
</declare-styleable>
<ImageView
android:layout_width="42dip"
android:layout_height="42dip"
android:background="@drawable/圖片ID"
/>

color —— 顏色值

<declare-styleable name="名稱">
<attr name="textColor" format="color"/>
</declare-styleable>
<TextView
android:layout_width="42dip"
android:layout_height="42dip"
android:textColor="#00FF00"
/>

boolean —— 布林值

<declare-styleable name="名稱">
<attr name="focusable" format="boolean"/>
</declare-styleable>
<Button
android:layout_width="42dip"
android:layout_height="42dip"
android:focusable="true"
/>

dimension —— 尺寸值

<declare-styleable name="名稱">
<attr name="layout_width" format="dimension"/>
</declare-styleable>
<Button
android:layout_width="42dip"
android:layout_height="42dip"
/>

float —— 浮點數

<declare-styleable name="AlphaAnimation">
<attr name="fromAlpha" format="float"/>
<attr name="toAlpha" format="float"/>
</declare-styleable>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.7"
/>

integer —— 整數

<declare-styleable name="AnimatedRotateDrawable">
<attr name="visible"/>
<attr name="frameDuration" format="integer"/>
<attr name="framesCount" format="integer"/>
<attr name="pivotX"/>
<attr name="pivotY"/>
<attr name="drawable"/>
</declare-styleable>
<animated-rotate
xmlns:android="https://schemas.android.com/apk/res/android"
android:drawable="@drawable/圖片ID"
android:pivotX="50%"
android:pivotY="50%"
android:framesCount="12"
android:frameDuration="100"
/>

string —— 字串

<declare-styleable name="MapView">
<attr name="apiKey" format="string"/>
</declare-styleable>
<com.google.android.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"
/>

fraction —— 百分比

<declare-styleable name="RotateDrawable">
<attr name="visible"/>
<attr name="fromDegrees" format="float"/>
<attr name="toDegrees" format="float"/>
<attr name="pivotX" format="fraction"/>
<attr name="pivotY" format="fraction"/>
<attr name="drawable"/>
</declare-styleable>
<rotate
xmlns:android="https://schemas.android.com/apk/res/android"
android:interpolator="@anim/動畫ID"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="200%"
android:pivotY="300%"
android:duration="5000"
android:repeatMode="restart"
android:repeatCount="infinite"
/>

enum —— 列舉值

<declare-styleable name="名稱">
<attr name="orientation">
<enum name="horizontal" value="0"/>
<enum name="vertical" value="1"/>
</attr>
</declare-styleable>
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>

flag —— 位元旗標,可用 | 組合

<declare-styleable name="名稱">
<attr name="windowSoftInputMode">
<flag name="stateUnspecified" value="0"/>
<flag name="stateUnchanged" value="1"/>
<flag name="stateHidden" value="2"/>
<flag name="stateAlwaysHidden" value="3"/>
<flag name="stateVisible" value="4"/>
<flag name="stateAlwaysVisible" value="5"/>
<flag name="adjustUnspecified" value="0x00"/>
<flag name="adjustResize" value="0x10"/>
<flag name="adjustPan" value="0x20"/>
<flag name="adjustNothing" value="0x30"/>
</attr>
</declare-styleable>
<activity
android:name=".StyleAndThemeActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateUnspecified|stateUnchanged | stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

一個屬性可以有多種型別

宣告時用 | 串接多個 format,使用時就能自由混填:

<declare-styleable name="名稱">
<attr name="background" format="reference|color"/>
</declare-styleable>
<ImageView
android:layout_width="42dip"
android:layout_height="42dip"
android:background="@drawable/圖片ID|#00FF00"
/>

AttributeSet 與 TypedArray 的分工

這兩個類別是自定義屬性在程式碼端的入口,釐清它們的角色是理解整個機制的關鍵。

AttributeSet 負責「裝」。控制項初始化的時候,系統會把佈局檔中屬於這個控制項的屬性名(key,例如 background)與對應的值(value,例如 @drawable/icon)打包成 AttributeSet,傳進 View 的建構子。對於非 Android 自帶的屬性,View 基類無法識別,必須由我們自己解析。

TypedArray 負責「說明」。AttributeSet 裡只有名稱和值,但控制項怎麼知道哪個屬性代表什麼意義、該用哪種型別去讀?這件事由 TypedArray 承擔——它封裝了 attrs.xml 中 styleable 所宣告的型別資訊,讓我們能把 AttributeSet 中的原始值正確取出並套用。

打個比方:AttributeSet 是一盒糖,TypedArray 則是盒上的標籤說明,告訴你每顆糖是什麼口味。而這盒糖究竟有哪些口味,取決於你在自己的 styleable 檔裡宣告了什麼。

程式碼端的典型寫法

理解概念之後,實際使用只需三步:在拿到 AttributeSet 的建構子裡呼叫 context.obtainStyledAttributes(attrs, R.styleable.你的屬性組) 取得 TypedArray,接著用 getColor / getDimension / getString 等方法依序讀值,最後別忘了呼叫 recycle() 釋放資源,否則會造成記憶體浪費。

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