我們自定義控件的時候,最常見的需要重寫構造函數是 public View(Context context) {}
和 public View(Context context, @Nullable AttributeSet attrs) {}
, 因為第一個是在 Java 代碼中創建 View 時會調用,第二是在 XML 布局文件中引用 View 時,會被調用。這兩個構造函數都是系統會調用的方法。但是,還剩兩個構造函數`` public View(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {}和
public View(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {}`, 是需要我們自行調用的,系統并不會調用,當我們需要為自定義 View 設置默認的屬性的時候,就需要用到了。
這是一段典型的一段自定義 View 的構造函數。
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();
}
}
可以看出來,屬性的獲取主要由 context.obtainStyledAttributes
決定,而最后兩個參數defStyleAttr
和defStyleRes
決定了最終默認的屬性樣式是怎么的。
為了弄清楚默認樣式的運作規律,我們先為這個自定義 View 創建 5 個自定義屬性,和一個單獨的屬性,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
設置 5
個屬性的原因是 Android 的 View 屬性覆蓋的優先級有 5
處,分別是:
- 布局文件中的直接指定的屬性
- 布局文件中的 style 中指定的屬性
- 我們自己定義的 Style
- Theme 中直接指定的 Style 引用
- Theme 中直接指定的屬性
他們之間的覆蓋的優先級是:
1 > 2 > 3 / 4 > 5
為了驗證我們的結論,首先在Application 或 Activity 的 Theme 中直接設置這5個屬性,并且為上方定義的 defaultStyleAttr
屬性設置一個style的引用,值為下方定義的 @style/DefaultStyleInAppTheme
, 而這個屬性將作為自定義View構造函數中的第三個值,即該自定義 View 默認的 style。DefaultStyleRes
則作為自定義 View 的構造函數中的第四個參數,而 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
然后我們自定義一個 TextView, 分別獲取這 5 個屬性的值,并且顯示出來。
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();
}
}
我們知道,屬性的獲取最終是根據 obtainStyledAttributes()
方法獲得,他的最后兩個參數也就是構造行數的最后兩個參數,這兩個參數都是設置自定義 View 的默認樣式,那么,他們作用的優先級是怎樣的?
* @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.
根據 API 文檔的可以看出,當 defStyleAttr
參數被設置為 0
, 或者在 Application 和 Activity 的 Theme 中找不到 defStyleAttr
屬性的賦值(例如 styles.xml 中的 <item name="defaultStyleAttr">@style/DefaultStyleInAppTheme</item>
這行代碼被刪除),那么,這個 View 的默認樣式就是 defStyleRes
參數所引用的屬性樣式。可以看出這兩個參數是沖突的,而且沖突級別是整個 style, 即 defStyleAttr
參數的 style 能找到情況下就忽略了整個 defStyleRes
的屬性集。
然后,在布局文件中添加自定義的 View,分別設置了直接的屬性,firstAttr
和 設置了 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>
運行結果如下圖,可以看到 defStyleRes
所引用的 style 中的屬性完全被忽略了,因為 styles.xml
中能找到 defStyleAttr
這個屬性的賦值:
現在將 styles.xml
中能找到 defStyleAttr
這個屬性的賦值刪除,或者將 defStyleAttr
參數使用 0
賦值, 運行結果如下,可以看到 defStyleRes
所引用的style 才顯示出來:
實驗結果證明,屬性層級的覆蓋優先級是 1 > 2 > 3 / 4 > 5
,和上方結論一致。需要注意的是,在 style 層級的優先級 defStyleAttr
參數所引用的 style 的優先級是高于 defStyleRes
的。