我們在自定義View 時,一般都會用到
TypedArray obtainStyledAttributes(
AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes); 對於它的前面2個參數可能大家都知道, 對於後面兩個參數一直不明白怎麼用。
於是今天查了下。
context obtainStyledAttributes 最終會調到Resources.Theme.obtainStyledAttributes(AttributeSet set,
int[] attrs, int defStyleAttr, int defStyleRes)
再看一下參數解釋:
* @param set The base set of attribute values. May be null.
* @param attrs The desired attributes to be retrieved.
* @param defStyleAttr An attribute in the current theme that contains a
* reference to a style resource that supplies
* defaults values for the TypedArray. Can be
* 0 to not look for defaults.
* @param defStyleRes A resource identifier of a style resource that
* supplies default values for the TypedArray,
* used only if defStyleAttr is 0 or can not be found
* in the theme. Can be 0 to not look for defaults.
defStyleAttr 指向當前theme 某個item 描述的style 該style指定了一些默認值爲這個TypedArray
defStyleRes 當defStyleAttr 找不到或者爲0, 可以直接指定某個style
不是很理解 ,搜了一下源碼:
發現用的地方 不多。如CalendarView
TypedArray attributesArray = context.obtainStyledAttributes(attrs, R.styleable.CalendarView,
R.attr.calendarViewStyle, 0);
CalendarView 在 attrs.xml 中 定義如下:
<declare-styleable name="CalendarView">
<!-- The first day of week according to {@link java.util.Calendar}. -->
<attr name="firstDayOfWeek" format="integer" />
<!-- Whether do show week numbers. -->
<attr name="showWeekNumber" format="boolean" />
<!-- The minimal date shown by this calendar view in mm/dd/yyyy format. -->
<attr name="minDate" />
<!-- The minimal date shown by this calendar view in mm/dd/yyyy format. -->
<attr name="maxDate" />
<!-- The number of weeks to be shown. -->
<attr name="shownWeekCount" format="integer"/>
<!-- The background color for the selected week. -->
<attr name="selectedWeekBackgroundColor" format="color|reference" />
<!-- The color for the dates of the focused month. -->
<attr name="focusedMonthDateColor" format="color|reference" />
<!-- The color for the dates of an unfocused month. -->
<attr name="unfocusedMonthDateColor" format="color|reference" />
<!-- The color for the week numbers. -->
<attr name="weekNumberColor" format="color|reference" />
<!-- The color for the separator line between weeks. -->
<attr name="weekSeparatorLineColor" format="color|reference" />
<!-- Drawable for the vertical bar shown at the beginning and at the end of the selected date. -->
<attr name="selectedDateVerticalBar" format="reference" />
<!-- The text appearance for the week day abbreviation of the calendar header. -->
<attr name="weekDayTextAppearance" format="reference" />
<!-- The text appearance for the calendar dates. -->
<attr name="dateTextAppearance" format="reference" />
</declare-styleable>
默認 defStyleAttr 爲 calendarViewStyle
也定義在attrs.xml
<!-- The CalendarView style. -->
<attr name="calendarViewStyle" format="reference" />
再看看在theme.xml 中 它指向:
<!-- CalendarView style--Defalut>
<item name="calendarViewStyle">@style/Widget.CalendarView</item>
。。。。
<!-- CalendarView style--Holo>
<item name="calendarViewStyle">@style/Widget.Holo.CalendarView</item>
。。。
<!-- CalendarView style-HOLOLight->
<item name="calendarViewStyle">@style/Widget.Holo.Light.CalendarView</item>
不同的theme 指向不同的style
如style.xml 中的
<style name="Widget.CalendarView">
<item name="android:showWeekNumber">true</item>
<item name="android:minDate">01/01/1900</item>
<item name="android:maxDate">12/31/2100</item>
<item name="android:shownWeekCount">6</item>
<item name="android:selectedWeekBackgroundColor">#330099FF</item>
<item name="android:focusedMonthDateColor">#FFFFFFFF</item>
<item name="android:unfocusedMonthDateColor">#66FFFFFF</item>
<item name="android:weekNumberColor">#33FFFFFF</item>
<item name="android:weekSeparatorLineColor">#19FFFFFF</item>
<item name="android:selectedDateVerticalBar">@android:drawable/day_picker_week_view_dayline_holo</item>
<item name="android:weekDayTextAppearance">@android:style/TextAppearance.Small.CalendarViewWeekDayView</item>
<item name="android:dateTextAppearance">?android:attr/textAppearanceSmall</item>
</style>
指定了CalendarView 的atrrs 中的一些默認值。
自此我們明白了defStyleAttr的用法。 如果某個stylable 定義了一些item , 但是使用者並不一定對所有的item 在xml 使用時設置。 我們需要給他設定一些默認值
這些值可以在不同的theme 中不一樣。
defStyleRes 使用則更簡單。可以直接某個style, 當defStyleAttr不起作用時。