編輯:關於 Android 程式設計
系統自帶的 View 可以在 XML 中配置屬性,寫得好的自定義 View 同樣可以在 XML 中配置屬性。要讓自定義 View 的屬性能在 XML 中配置,需要四個步驟:
怎麼把屬性值應用到 View 就不必多說了,接下來看前三點。
透過 <declare-styleable> 元素聲明自定義 View 需要的屬性即可。以下是一個例子,檔案為 res/values/attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Customize">
<attr name="attr_one" format="string" />
<attr name="attr_two" format="string" />
<attr name="attr_three" format="string" />
<attr name="attr_four" format="string" />
</declare-styleable>
<attr name="CustomizeStyle" format="reference" />
</resources>
在上述 XML 中,我們聲明了 Customize 與 CustomizeStyle。Customize 包含 attr_one、attr_two、attr_three、attr_four 四個 attribute,CustomizeStyle 也是一個 attribute,但沒有聲明在 declare-styleable 中。
定義在 declare-styleable 中與直接用 attr 定義沒有實質不同,系統都會為我們在 R.attr 中生成 5 個 attribute:
public static final class attr {
public static final int CustomizeStyle = 0x7f010004;
public static final int attr_one = 0x7f010000;
public static final int attr_two = 0x7f010001;
public static final int attr_three = 0x7f010002;
public static final int attr_four = 0x7f010003;
}
不同的是,若聲明在 declare-styleable 中,系統還會在 R.styleable 中生成相關屬性:
public static final class styleable {
public static final int[] Customize = {
0x7f010000, 0x7f010001, 0x7f010002, 0x7f010003
};
public static final int Customize_attr_one = 0;
public static final int Customize_attr_two = 1;
public static final int Customize_attr_three = 2;
public static final int Customize_attr_four = 3;
}
R.styleable.Customize 是一個 int[],裡面的元素正好與 R.attr.attr_one 至 attr_four 一一對應,而 Customize_attr_one 等四個值就是它們在數組中的索引。這個數組與索引在第三步運行時獲取屬性值時會用到。將 attr 分組聲明在 declare-styleable 中的作用,就是系統會自動生成這些東西;若不聲明,也可以自行構建數組,只是比較麻煩。
在 XML 中為屬性賦值有幾種不同方式:直接在 layout 中使用屬性、設置 style 並在 style 中設置屬性、透過 Application 或 Activity 指定 theme 並在其中設置預設值。下面分別說明。
自定義屬性所屬的 namespace 與系統屬性不同,需像聲明 android namespace 一樣聲明當前程式的 namespace(把 android 換成當前程式的套件名稱):
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:ad="https://schemas.android.com/apk/res/com.angeldevil.customstyle"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<com.angeldevil.customstyle.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ad:attr_one="attr one in xml"
style="@style/ThroughStyle"
android:text="@string/hello_world" />
</RelativeLayout>
我們為 CustomTextView 聲明了一個 style:ThroughStyle,它指定了兩個屬性的值:
<style name="ThroughStyle">
<item name="attr_one">attr one from style</item>
<item name="attr_two">attr two from style</item>
</style>
注意:在 style 中聲明了 attr_one 的值,同時 XML 中也直接向 attr_one 賦了值,最終用哪一個涉及優先級問題,後面再說明。
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="attr_one">attr one from theme</item>
<item name="attr_two">attr two from theme</item>
<item name="attr_three">attr three from theme</item>
<item name="CustomizeStyle">@style/CustomizeStyleInTheme</item>
</style>
<style name="CustomizeStyleInTheme">
<item name="attr_one">attr one from theme reference</item>
<item name="attr_two">attr two from theme reference</item>
<item name="attr_three">attr three from theme reference</item>
</style>
我們在 AppTheme 中為多個屬性指定了值,同時也為 CustomizeStyle 指定了一個 reference,並在該 reference 中為屬性指定值。在 theme 中設值有兩種方式:直接定義,或透過另一個 attribute 引用一個 style,這兩種方式在獲取值時有區別。
先定義一個 DefaultCustomizeStyle,其作用稍後可見:
<style name="DefaultCustomizeStyle">
<item name="attr_one">attr one from defalut style res</item>
<item name="attr_two">attr two from defalut style res</item>
<item name="attr_three">attr three from defalut style res</item>
</style>
CustomTextView 的程式碼:
public class CustomTextView extends TextView {
private static final String TAG = CustomTextView.class.getSimpleName();
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.CustomizeStyle);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Customize, defStyle, R.style.DefaultCustomizeStyle);
String one = a.getString(R.styleable.Customize_attr_one);
String two = a.getString(R.styleable.Customize_attr_two);
String three = a.getString(R.styleable.Customize_attr_three);
String four = a.getString(R.styleable.Customize_attr_four);
Log.i(TAG, "one:" + one);
Log.i(TAG, "two:" + two);
Log.i(TAG, "three:" + three);
Log.i(TAG, "four:" + four);
a.recycle();
}
}
主要程式碼都在第三個函數中,透過 Context 的 obtainStyledAttributes 獲得前面定義的四個屬性值並印出來。
View 第三個構造函數的第三個參數 defStyle
在程式碼中實例化 View 會呼叫第一個構造函數,在 XML 中定義會呼叫第二個,而第三個函數系統不會呼叫,需由 View(自定義或系統預定義)顯式呼叫。此處我們在第二個構造函數中呼叫第三個,並將 R.attr.CustomizeStyle 傳給第三個參數。
第三個參數的意義正如其名,是預設的 Style——具體而言,是指它在當前 Application 或 Activity 所用 Theme 中的預設 Style。以系統 Button 為例:
public Button(Context context) {
this(context, null);
}
public Button(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.buttonStyle);
}
public Button(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
系統預設有一個 Theme(如 4.0 中的 Theme.Holo),為 buttonStyle 指定了一個 Style:
<style name="Theme.DeviceDefault" parent="Theme.Holo">
...
<item name="buttonStyle">@android:style/Widget.DeviceDefault.Button</item>
...
</style>
<style name="Widget.DeviceDefault.Button" parent="Widget.Holo.Button"></style>
<style name="Widget.Holo.Button" parent="Widget.Button">
<item name="android:background">@android:drawable/btn_default_holo_dark</item>
<item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
<item name="android:textColor">@android:color/primary_text_holo_dark</item>
<item name="android:minHeight">48dip</item>
<item name="android:minWidth">64dip</item>
</style>
從文檔中可看到,第三個構造函數的作用是為 View 子類提供基礎樣式:Perform inflation from XML and apply a class-specific base style。
obtainStyledAttributes
要獲取的屬性值都透過這個函數返回的 TypedArray 獲得,函數原型:
public TypedArray obtainStyledAttributes(AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)
四個參數分別是:
set:屬性值的集合。
attrs:要獲取的屬性資源 ID 數組,類似查詢資料庫時的 Projection,表示從一堆屬性中查詢哪些屬性的值。
defStyleAttr:當前 Theme 中的一個 attribute,指向 style 的引用。當 layout XML 與 style 中都沒為 View 指定屬性時,會從 Theme 中該 attribute 指向的 Style 查找相應值;若傳入 0 則不向 Theme 搜索預設值。
defStyleRes:指向一個 Style 的資源 ID,僅在 defStyleAttr 為 0,或 defStyleAttr 不為 0 但 Theme 中沒為它賦值時才起作用。
對於一個屬性,可以在多個地方指定值(XML 直接定義、style、Theme),它們的優先級由高到低依次是:
直接在 XML 中定義 > style 定義 > 由 defStyleAttr 和 defStyleRes 指定的預設值 > 直接在 Theme 中指定的值。
掌握這個優先級關係,就能準確理解自定義 View 屬性值的取值來源與覆蓋規則。
深入源碼剖析 AsyncTask 的完整運作機制
什麼是 AsyncTask AsyncTask 即 asynchronous task,異步任務。它實際上是圍繞 Thread 與 Handler 設計的一個輔助類,內部
Android 性能測試指南:卡頓、ANR、流暢度與 SmartMonkey
Android 性能測試一直是許多團隊既熟悉又頭痛的主題。它不像功能測試那樣有明確的通過與失敗,反而牽涉到機型差異、系統版本、使用者操作習慣與應用本身的實作品質。本文整理
GreenDAO 入門(一):快速把 Java 對象映射到 SQLite
做 Android 開發很難避開 SQLite。如果不用任何框架,我們得自己拼 SQL 語句、自己解析 Cursor 查詢結果,既繁瑣又容易出錯。好消息是市面上已有不少成
Android 圖片詳解(4):透視 Bitmap 與 Drawable 的底層關係
延續前一篇的內容,這次我們把焦點放在 Bitmap 與 Drawable 這兩個最常見的圖片載體上,透過一組實驗程式碼來釐清它們之間的關係與差異。 先來看看測試用的程式碼