我們自定義控件的時(shí)候缸逃,最常見(jiàn)的需要重寫(xiě)構(gòu)造函數(shù)是 public View(Context context) {}
和 public View(Context context, @Nullable AttributeSet attrs) {}
, 因?yàn)榈谝粋€(gè)是在 Java 代碼中創(chuàng)建 View 時(shí)會(huì)調(diào)用耕突,第二是在 XML 布局文件中引用 View 時(shí),會(huì)被調(diào)用芹橡。這兩個(gè)構(gòu)造函數(shù)都是系統(tǒng)會(huì)調(diào)用的方法毒坛。但是,還剩兩個(gè)構(gòu)造函數(shù)`` public View(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {}和
public View(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {}`, 是需要我們自行調(diào)用的林说,系統(tǒng)并不會(huì)調(diào)用煎殷,當(dāng)我們需要為自定義 View 設(shè)置默認(rèn)的屬性的時(shí)候,就需要用到了腿箩。
這是一段典型的一段自定義 View 的構(gòu)造函數(shù)豪直。
public class AttrTestTextView extends TextView {
public AttrTestTextView(Context context) {
super(context);
}
public AttrTestTextView(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.defaultStyleAttr);
}
public AttrTestTextView(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, R.style.DefaultStyleRes);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public AttrTestTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AttrTestTextView,
defStyleAttr, defStyleRes);
...
typedArray.recycle();
}
}
可以看出來(lái),屬性的獲取主要由 context.obtainStyledAttributes
決定珠移,而最后兩個(gè)參數(shù)defStyleAttr
和defStyleRes
決定了最終默認(rèn)的屬性樣式是怎么的弓乙。
為了弄清楚默認(rèn)樣式的運(yùn)作規(guī)律末融,我們先為這個(gè)自定義 View 創(chuàng)建 5 個(gè)自定義屬性,和一個(gè)單獨(dú)的屬性暇韧,attrs.xml
文件如下:
<resources>
<declare-styleable name="AttrTestTextView">
<attr name="firstAttr" format="string" />
<attr name="secondAttr" format="string" />
<attr name="thirdAttr" format="string" />
<attr name="fourthAttr" format="string" />
<attr name="fifthAttr" format="string" />
</declare-styleable>
<attr name="defaultStyleAttr" format="reference" />
</resources
設(shè)置 5
個(gè)屬性的原因是 Android 的 View 屬性覆蓋的優(yōu)先級(jí)有 5
處勾习,分別是:
- 布局文件中的直接指定的屬性
- 布局文件中的 style 中指定的屬性
- 我們自己定義的 Style
- Theme 中直接指定的 Style 引用
- Theme 中直接指定的屬性
他們之間的覆蓋的優(yōu)先級(jí)是:
1 > 2 > 3 / 4 > 5
為了驗(yàn)證我們的結(jié)論,首先在Application 或 Activity 的 Theme 中直接設(shè)置這5個(gè)屬性懈玻,并且為上方定義的 defaultStyleAttr
屬性設(shè)置一個(gè)style的引用巧婶,值為下方定義的 @style/DefaultStyleInAppTheme
, 而這個(gè)屬性將作為自定義View構(gòu)造函數(shù)中的第三個(gè)值,即該自定義 View 默認(rèn)的 style涂乌。DefaultStyleRes
則作為自定義 View 的構(gòu)造函數(shù)中的第四個(gè)參數(shù)艺栈,而 LayoutStyle
則在布局中直接引用, styles.xml
文件如下:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="firstAttr">firstAttr from theme attr</item>
<item name="secondAttr">secondAttr from theme attr</item>
<item name="thirdAttr">thirdAttr from theme attr</item>
<item name="fourthAttr">fourthAttr from theme attr</item>
<item name="fifthAttr">fifthAttr from theme attr</item>
<item name="defaultStyleAttr">@style/DefaultStyleInAppTheme</item>
</style>
<style name="DefaultStyleInAppTheme">
<item name="firstAttr">firstAttr from theme style</item>
<item name="secondAttr">secondAttr from theme style</item>
<item name="thirdAttr">thirdAttr from theme style</item>
</style>
<style name="DefaultStyleRes">
<item name="firstAttr">firstAttr from style res</item>
<item name="secondAttr">secondAttr from style res</item>
<item name="thirdAttr">thirdAttr from style res</item>
<item name="fourthAttr">fourthAttr from style res</item>
</style>
<style name="LayoutStyle">
<item name="firstAttr">firstAttr from layout style</item>
<item name="secondAttr">secondAttr from layout style</item>
</style>
</resources
然后我們自定義一個(gè) TextView湾盒, 分別獲取這 5 個(gè)屬性的值湿右,并且顯示出來(lái)。
public class AttrTestTextView extends TextView {
public AttrTestTextView(Context context) {
super(context);
}
public AttrTestTextView(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.defaultStyleAttr);
}
public AttrTestTextView(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, R.style.DefaultStyleRes);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public AttrTestTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AttrTestTextView,
defStyleAttr, defStyleRes);
StringBuilder sb = new StringBuilder();
sb.append(typedArray.getString(R.styleable.AttrTestTextView_firstAttr)).append("\n");
sb.append(typedArray.getString(R.styleable.AttrTestTextView_secondAttr)).append("\n");
sb.append(typedArray.getString(R.styleable.AttrTestTextView_thirdAttr)).append("\n");
sb.append(typedArray.getString(R.styleable.AttrTestTextView_fourthAttr)).append("\n");
sb.append(typedArray.getString(R.styleable.AttrTestTextView_fifthAttr));
setText(sb);
typedArray.recycle();
}
}
我們知道罚勾,屬性的獲取最終是根據(jù) obtainStyledAttributes()
方法獲得毅人,他的最后兩個(gè)參數(shù)也就是構(gòu)造行數(shù)的最后兩個(gè)參數(shù),這兩個(gè)參數(shù)都是設(shè)置自定義 View 的默認(rèn)樣式荧库,那么堰塌,他們作用的優(yōu)先級(jí)是怎樣的赵刑?
* @param defStyleRes A resource identifier of a style resource that
* supplies default values for the view, used only if
* defStyleAttr is 0 or can not be found in the theme. Can be 0
* to not look for defaults.
根據(jù) API 文檔的可以看出分衫,當(dāng) defStyleAttr
參數(shù)被設(shè)置為 0
, 或者在 Application 和 Activity 的 Theme 中找不到 defStyleAttr
屬性的賦值(例如 styles.xml 中的 <item name="defaultStyleAttr">@style/DefaultStyleInAppTheme</item>
這行代碼被刪除)般此,那么蚪战,這個(gè) View 的默認(rèn)樣式就是 defStyleRes
參數(shù)所引用的屬性樣式☆戆茫可以看出這兩個(gè)參數(shù)是沖突的邀桑,而且沖突級(jí)別是整個(gè) style, 即 defStyleAttr
參數(shù)的 style 能找到情況下就忽略了整個(gè) defStyleRes
的屬性集。
然后科乎,在布局文件中添加自定義的 View壁畸,分別設(shè)置了直接的屬性,firstAttr
和 設(shè)置了 styles.xml
中定義的 style茅茂。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.ryeeeeee.attrtest.AttrTestTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:firstAttr="firstAttr from layout attr"
style="@style/LayoutStyle"/>
</RelativeLayout>
運(yùn)行結(jié)果如下圖捏萍,可以看到 defStyleRes
所引用的 style 中的屬性完全被忽略了,因?yàn)?styles.xml
中能找到 defStyleAttr
這個(gè)屬性的賦值:
現(xiàn)在將 styles.xml
中能找到 defStyleAttr
這個(gè)屬性的賦值刪除空闲,或者將 defStyleAttr
參數(shù)使用 0
賦值, 運(yùn)行結(jié)果如下令杈,可以看到 defStyleRes
所引用的style 才顯示出來(lái):
實(shí)驗(yàn)結(jié)果證明,屬性層級(jí)的覆蓋優(yōu)先級(jí)是 1 > 2 > 3 / 4 > 5
碴倾,和上方結(jié)論一致逗噩。需要注意的是掉丽,在 style 層級(jí)的優(yōu)先級(jí) defStyleAttr
參數(shù)所引用的 style 的優(yōu)先級(jí)是高于 defStyleRes
的。