[转]Android学习系列(39-40)–Android主题和样式之系统篇

 

转自:

http://www.cnblogs.com/qianxudetianxia/p/3725466.html

http://www.cnblogs.com/qianxudetianxia/p/3996020.html

 

 

【基于最新的Android4.4的源码分析】

每家公司或者每个移动团队无不想开发出一套自己的UI框架,融入自己的设计和特性,这必然会去修改android的ui。
所以,学习和理解android的UI设计是最基础和非常有必要的。
android ui设计最重要的就是主题和样式。

1、位置
在Android的frameworks/base/core/res/res/values目录下有一下几个文件:

1
2
3
4
themes.xml
themes_device_defaults.xml
styles.xml
styles_device_defaults.xml

分别定义了各种系统Theme,Style。

2、主题Theme
主要关注themes.xml,themes_device_defaults.xml两个文件。
themes.xml定义了android低版本的theme和Holo theme,themes_device_defaults.xml定义了DeviceDefault主题(继承自Holo主题),实际上就是在Holo主题上定制主题(For厂商)。
系统如何去选择默认的主题呢?

<!–
===============================================================
PLEASE READ
===============================================================
This file contains the themes that are the Device Defaults.
If you want to edit themes to skin your device, do it here.
We recommend that you do not edit themes.xml and instead edit
this file.

Editing this file instead of themes.xml will greatly simplify
merges for future platform versions and CTS compliance will be
easier.
===============================================================
PLEASE READ
===============================================================
–>
<resources>

<!– The default theme for apps that target API level 14 and higher.
<p>The DeviceDefault themes are aliases for a specific device’s native look and feel. The
DeviceDefault theme family and widget style family offer ways for you to target your app
to a device’s native theme with all device customizations intact.</p>
<p>For example, when you set your app’s {@code targetSdkVersion} to 14 or higher, this
theme is applied to your application by default. As such, your app might appear with the
{@link #Theme_Holo Holo} styles on one device, but with a different set of styles on
another device. This is great if you want your app to fit with the device’s native look and
feel. If, however, you prefer to keep your UI style the same across all devices, you should
apply a specific theme such as {@link #Theme_Holo Holo} or one of your own design. For more
information, read <a
href=”http://android-developers.blogspot.com/2012/01/holo-everywhere.html”>Holo
Everywhere</a>.</p>
<p>Styles used by the DeviceDefault theme are named using the convention
Type.DeviceDefault.Etc (for example, {@code Widget.DeviceDefault.Button} and
{@code TextAppearance.DeviceDefault.Widget.PopupMenu.Large}).</p>
–>
<style name=”Theme.DeviceDefault” parent=”Theme.Holo” >

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**frameworks/base/core/java/android/content/res/Resources.java*/
/** @hide */
public static int selectDefaultTheme(int curTheme, int targetSdkVersion) {
    return selectSystemTheme(curTheme, targetSdkVersion,
            com.android.internal.R.style.Theme,
            com.android.internal.R.style.Theme_Holo,
            com.android.internal.R.style.Theme_DeviceDefault);
}   
/** @hide */
public static int selectSystemTheme(int curTheme, int targetSdkVersion,
        int orig, int holo, int deviceDefault) {
    if (curTheme != 0) {
        return curTheme;
    }   
    if (targetSdkVersion < Build.VERSION_CODES.HONEYCOMB) {
        // < 11
        return orig;
    }   
    if (targetSdkVersion < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
         // < 14
         return holo;
    }   
    return deviceDefault;
}

当<11时,使用以前低版本主题;当>=11&&<14,使用Holo主题;>14的时候,使用DeviceDefault主题。
方便理解,下面把目前所有的版本号列出来,也顺便温习一下android的历史:

3、系统主题Theme列表
系统默认大的主题是三种:Theme,Theme.Holo,Theme.DeviceDefault, 但是实际上在此基础系统还定义了大量的派生主题,最典型的是对应的Light主题。
除此之外,还有很多,在此一一列出,打字太痛苦了,我贴出截图:

了解android系统定义的主题之后,我们就可以根据实际情况在自己的应用中使用这些主题,但是如果想修改主题的某些内容,需要进一步深入。

4、详解每个主题中定义item分类
一个完整的主题应该定义哪些内容呢,以Theme为例,如下:
1)颜色

1
2
3
4
5
6
7
8
9
10
<item name="colorForeground">@android:color/bright_foreground_dark</item>
<item name="colorForegroundInverse">@android:color/bright_foreground_dark_inverse</item>
<item name="colorBackground">@android:color/background_dark</item>
<item name="colorBackgroundCacheHint">?android:attr/colorBackground</item>
<item name="colorPressedHighlight">@color/legacy_pressed_highlight</item>
<item name="colorLongPressedHighlight">@color/legacy_long_pressed_highlight</item>
<item name="colorFocusedHighlight">@color/legacy_selected_highlight</item>
<item name="colorMultiSelectHighlight">@color/legacy_selected_highlight</item>
<item name="colorActivatedHighlight">@color/legacy_selected_highlight</item>

2)字体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<!-- Text styles -->
<item name="textAppearance">@android:style/TextAppearance</item>
<item name="textAppearanceInverse">@android:style/TextAppearance.Inverse</item>
<item name="textColorPrimary">@android:color/primary_text_dark</item>
<item name="textColorSecondary">@android:color/secondary_text_dark</item>
<item name="textColorTertiary">@android:color/tertiary_text_dark</item>
<item name="textColorPrimaryInverse">@android:color/primary_text_light</item>
<item name="textColorSecondaryInverse">@android:color/secondary_text_light</item>
<item name="textColorTertiaryInverse">@android:color/tertiary_text_light</item>
<item name="textColorPrimaryDisableOnly">@android:color/primary_text_dark_disable_only</item>
<item name="textColorPrimaryInverseDisableOnly">@android:color/primary_text_light_disable_only</item>
<item name="textColorPrimaryNoDisable">@android:color/primary_text_dark_nodisable</item>
<item name="textColorSecondaryNoDisable">@android:color/secondary_text_dark_nodisable</item>
<item name="textColorPrimaryInverseNoDisable">@android:color/primary_text_light_nodisable</item>
<item name="textColorSecondaryInverseNoDisable">@android:color/secondary_text_light_nodisable</item>
<item name="textColorHint">@android:color/hint_foreground_dark</item>
<item name="textColorHintInverse">@android:color/hint_foreground_light</item>
<item name="textColorSearchUrl">@android:color/search_url_text</item>
<item name="textColorHighlight">@android:color/highlighted_text_dark</item>
<item name="textColorHighlightInverse">@android:color/highlighted_text_light</item>
<item name="textColorLink">@android:color/link_text_dark</item>
<item name="textColorLinkInverse">@android:color/link_text_light</item>
<item name="textColorAlertDialogListItem">@android:color/primary_text_light_disable_only</item>
<item name="textAppearanceLarge">@android:style/TextAppearance.Large</item>
<item name="textAppearanceMedium">@android:style/TextAppearance.Medium</item>
<item name="textAppearanceSmall">@android:style/TextAppearance.Small</item>
<item name="textAppearanceLargeInverse">@android:style/TextAppearance.Large.Inverse</item>
<item name="textAppearanceMediumInverse">@android:style/TextAppearance.Medium.Inverse</item>
<item name="textAppearanceSmallInverse">@android:style/TextAppearance.Small.Inverse</item>
<item name="textAppearanceSearchResultTitle">@android:style/TextAppearance.SearchResult.Title</item>
<item name="textAppearanceSearchResultSubtitle">@android:style/TextAppearance.SearchResult.Subtitle</item>
<item name="textAppearanceEasyCorrectSuggestion">@android:style/TextAppearance.EasyCorrectSuggestion</item>
<item name="textAppearanceMisspelledSuggestion">@android:style/TextAppearance.MisspelledSuggestion</item>
<item name="textAppearanceAutoCorrectionSuggestion">@android:style/TextAppearance.AutoCorrectionSuggestion</item>
<item name="textAppearanceButton">@android:style/TextAppearance.Widget.Button</item>
<item name="editTextColor">@android:color/primary_text_light</item>
<item name="editTextBackground">@android:drawable/edit_text</item>
<item name="candidatesTextStyleSpans">@android:string/candidates_style</item>
<item name="textCheckMark">@android:drawable/indicator_check_mark_dark</item>
<item name="textCheckMarkInverse">@android:drawable/indicator_check_mark_light</item>
<item name="textAppearanceLargePopupMenu">@android:style/TextAppearance.Widget.PopupMenu.Large</item>
<item name="textAppearanceSmallPopupMenu">@android:style/TextAppearance.Widget.PopupMenu.Small</item>

3)按钮

1
2
3
4
5
6
7
8
9
10
11
<!-- Button styles -->
<item name="buttonStyle">@android:style/Widget.Button</item>
<item name="buttonStyleSmall">@android:style/Widget.Button.Small</item>
<item name="buttonStyleInset">@android:style/Widget.Button.Inset</item>
<item name="buttonStyleToggle">@android:style/Widget.Button.Toggle</item>
<item name="selectableItemBackground">@android:drawable/item_background</item>
<item name="borderlessButtonStyle">?android:attr/buttonStyle</item>
<item name="homeAsUpIndicator">@android:drawable/ic_ab_back_holo_dark</item>

4)List

1
2
3
4
5
6
7
8
9
10
11
<!-- List attributes -->
<item name="listPreferredItemHeight">64dip</item>
<item name="listPreferredItemHeightSmall">?android:attr/listPreferredItemHeight</item>
<item name="listPreferredItemHeightLarge">?android:attr/listPreferredItemHeight</item>
<item name="dropdownListPreferredItemHeight">?android:attr/listPreferredItemHeight</item>
<item name="textAppearanceListItem">?android:attr/textAppearanceLarge</item>
<item name="textAppearanceListItemSmall">?android:attr/textAppearanceLarge</item>
<item name="listPreferredItemPaddingLeft">6dip</item>
<item name="listPreferredItemPaddingRight">6dip</item>
<item name="listPreferredItemPaddingStart">6dip</item>
<item name="listPreferredItemPaddingEnd">6dip</item>

5)Window

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!-- Window attributes -->
<item name="windowBackground">@android:drawable/screen_background_selector_dark</item>
<item name="windowFrame">@null</item>
<item name="windowNoTitle">false</item>
<item name="windowFullscreen">false</item>
<item name="windowOverscan">false</item>
<item name="windowIsFloating">false</item>
<item name="windowContentOverlay">@null</item>
<item name="windowShowWallpaper">false</item>
<item name="windowTitleStyle">@android:style/WindowTitle</item>
<item name="windowTitleSize">25dip</item>
<item name="windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Activity</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustUnspecified</item>
<item name="windowActionBar">false</item>
<item name="windowActionModeOverlay">false</item>
<item name="windowCloseOnTouchOutside">false</item>
<item name="windowTranslucentStatus">false</item>
<item name="windowTranslucentNavigation">false</item>

6)Dialog

1
2
3
4
5
<!-- Dialog attributes -->
<item name="dialogTheme">@android:style/Theme.Dialog</item>
<item name="dialogTitleIconsDecorLayout">@layout/dialog_title_icons</item>
<item name="dialogCustomTitleDecorLayout">@layout/dialog_custom_title</item>
<item name="dialogTitleDecorLayout">@layout/dialog_title</item>

7)AlertDialog

1
2
3
4
5
<!-- AlertDialog attributes -->
<item name="alertDialogTheme">@android:style/Theme.Dialog.Alert</item>
<item name="alertDialogStyle">@android:style/AlertDialog</item>
<item name="alertDialogCenterButtons">true</item>
<item name="alertDialogIcon">@android:drawable/ic_dialog_alert</item>

8)Panel

1
2
3
4
5
6
7
8
9
10
<!-- Panel attributes -->
<item name="panelBackground">@android:drawable/menu_background</item>
<item name="panelFullBackground">@android:drawable/menu_background_fill_parent_width</item>
<!-- These three attributes do not seems to be used by the framework. Declared public though -->
<item name="panelColorBackground">#000</item>
<item name="panelColorForeground">?android:attr/textColorPrimary</item>
<item name="panelTextAppearance">?android:attr/textAppearance</item>
<item name="panelMenuIsCompact">false</item>
<item name="panelMenuListWidth">296dip</item>

9)滚动条(Scrollbar)

1
2
3
4
5
6
7
8
<!-- Scrollbar attributes -->
<item name="scrollbarFadeDuration">250</item>
<item name="scrollbarDefaultDelayBeforeFade">300</item>
<item name="scrollbarSize">10dip</item>
<item name="scrollbarThumbHorizontal">@android:drawable/scrollbar_handle_horizontal</item>
<item name="scrollbarThumbVertical">@android:drawable/scrollbar_handle_vertical</item>
<item name="scrollbarTrackHorizontal">@null</item>
<item name="scrollbarTrackVertical">@null</item>

10)文字选中(Text selection)

1
2
3
4
5
6
7
8
9
10
11
12
<!-- Text selection handle attributes -->
<item name="textSelectHandleLeft">@android:drawable/text_select_handle_left</item>
<item name="textSelectHandleRight">@android:drawable/text_select_handle_right</item>
<item name="textSelectHandle">@android:drawable/text_select_handle_middle</item>
<item name="textSelectHandleWindowStyle">@android:style/Widget.TextSelectHandle</item>
<item name="textEditPasteWindowLayout">@android:layout/text_edit_paste_window</item>
<item name="textEditNoPasteWindowLayout">@android:layout/text_edit_no_paste_window</item>
<item name="textEditSidePasteWindowLayout">@android:layout/text_edit_side_paste_window</item>
<item name="textEditSideNoPasteWindowLayout">@android:layout/text_edit_side_no_paste_window</item>
<item name="textSuggestionsWindowStyle">@android:style/Widget.TextSuggestionsPopupWindow</item>
<item name="textEditSuggestionItemLayout">@android:layout/text_edit_suggestion_item</item>
<item name="textCursorDrawable">@null</item>

有点长,下篇继续列举,并深入分析具体style.

 

11)Widget样式(Widget Style)

特别说明,此处定义大量的系统内置控件的样式,对于重写原生控件的样式具有很大的参考价值。

        <!-- Widget styles -->
        <item name="absListViewStyle">@android:style/Widget.AbsListView</item>
        <item name="autoCompleteTextViewStyle">@android:style/Widget.AutoCompleteTextView</item>        
        <item name="checkboxStyle">@android:style/Widget.CompoundButton.CheckBox</item>
        <item name="checkedTextViewStyle">@android:style/Widget.CheckedTextView</item>
        <item name="dropDownListViewStyle">@android:style/Widget.ListView.DropDown</item>
        <item name="editTextStyle">@android:style/Widget.EditText</item>
        <item name="expandableListViewStyle">@android:style/Widget.ExpandableListView</item>
        <item name="expandableListViewWhiteStyle">@android:style/Widget.ExpandableListView.White</item>
        <item name="galleryStyle">@android:style/Widget.Gallery</item>
        <item name="gestureOverlayViewStyle">@android:style/Widget.GestureOverlayView</item>
        <item name="gridViewStyle">@android:style/Widget.GridView</item>
        <item name="imageButtonStyle">@android:style/Widget.ImageButton</item>
        <item name="imageWellStyle">@android:style/Widget.ImageWell</item>
        <item name="listViewStyle">@android:style/Widget.ListView</item>
        <item name="listViewWhiteStyle">@android:style/Widget.ListView.White</item>
        <item name="popupWindowStyle">@android:style/Widget.PopupWindow</item>
        <item name="progressBarStyle">@android:style/Widget.ProgressBar</item>
        <item name="progressBarStyleHorizontal">@android:style/Widget.ProgressBar.Horizontal</item>
        <item name="progressBarStyleSmall">@android:style/Widget.ProgressBar.Small</item>
        <item name="progressBarStyleSmallTitle">@android:style/Widget.ProgressBar.Small.Title</item>
        <item name="progressBarStyleLarge">@android:style/Widget.ProgressBar.Large</item>
        <item name="progressBarStyleInverse">@android:style/Widget.ProgressBar.Inverse</item>
        <item name="progressBarStyleSmallInverse">@android:style/Widget.ProgressBar.Small.Inverse</item>
        <item name="progressBarStyleLargeInverse">@android:style/Widget.ProgressBar.Large.Inverse</item>
        <item name="seekBarStyle">@android:style/Widget.SeekBar</item>
        <item name="ratingBarStyle">@android:style/Widget.RatingBar</item>
        <item name="ratingBarStyleIndicator">@android:style/Widget.RatingBar.Indicator</item>
        <item name="ratingBarStyleSmall">@android:style/Widget.RatingBar.Small</item>
        <item name="radioButtonStyle">@android:style/Widget.CompoundButton.RadioButton</item>
        <item name="scrollViewStyle">@android:style/Widget.ScrollView</item>
        <item name="horizontalScrollViewStyle">@android:style/Widget.HorizontalScrollView</item>
        <item name="spinnerStyle">@android:style/Widget.Spinner</item>
        <item name="dropDownSpinnerStyle">@android:style/Widget.Spinner.DropDown</item>
        <item name="starStyle">@android:style/Widget.CompoundButton.Star</item>
        <item name="tabWidgetStyle">@android:style/Widget.TabWidget</item>
        <item name="textViewStyle">@android:style/Widget.TextView</item>
        <item name="errorMessageBackground">@android:drawable/popup_inline_error</item>
        <item name="errorMessageAboveBackground">@android:drawable/popup_inline_error_above</item>
        <item name="webTextViewStyle">@android:style/Widget.WebTextView</item>
        <item name="webViewStyle">@android:style/Widget.WebView</item>
        <item name="dropDownItemStyle">@android:style/Widget.DropDownItem</item>
        <item name="spinnerDropDownItemStyle">@android:style/Widget.DropDownItem.Spinner</item>
        <item name="spinnerItemStyle">@android:style/Widget.TextView.SpinnerItem</item>
        <item name="dropDownHintAppearance">@android:style/TextAppearance.Widget.DropDownHint</item>
        <item name="keyboardViewStyle">@android:style/Widget.KeyboardView</item>
        <item name="quickContactBadgeOverlay">@android:drawable/quickcontact_badge_overlay_dark</item>
        <item name="quickContactBadgeStyleWindowSmall">@android:style/Widget.QuickContactBadge.WindowSmall</item>
        <item name="quickContactBadgeStyleWindowMedium">@android:style/Widget.QuickContactBadge.WindowMedium</item>
        <item name="quickContactBadgeStyleWindowLarge">@android:style/Widget.QuickContactBadge.WindowLarge</item>
        <item name="quickContactBadgeStyleSmallWindowSmall">@android:style/Widget.QuickContactBadgeSmall.WindowSmall</item>
        <item name="quickContactBadgeStyleSmallWindowMedium">@android:style/Widget.QuickContactBadgeSmall.WindowMedium</item>
        <item name="quickContactBadgeStyleSmallWindowLarge">@android:style/Widget.QuickContactBadgeSmall.WindowLarge</item>
        <item name="listPopupWindowStyle">@android:style/Widget.ListPopupWindow</item>
        <item name="popupMenuStyle">@android:style/Widget.PopupMenu</item>
        <item name="activityChooserViewStyle">@android:style/Widget.ActivityChooserView</item>

        <item name="mediaRouteButtonStyle">@android:style/Widget.DeviceDefault.MediaRouteButton</item>
12)Preference样式(Preference Style)

说明,设置类界面样式

        <!-- Preference styles -->
        <item name="preferenceScreenStyle">@android:style/Preference.PreferenceScreen</item>
        <item name="preferenceFragmentStyle">@style/PreferenceFragment</item>
        <item name="preferenceCategoryStyle">@android:style/Preference.Category</item>
        <item name="preferenceStyle">@android:style/Preference</item>
        <item name="preferenceInformationStyle">@android:style/Preference.Information</item>
        <item name="checkBoxPreferenceStyle">@android:style/Preference.CheckBoxPreference</item>
        <item name="switchPreferenceStyle">@android:style/Preference.SwitchPreference</item>
        <item name="yesNoPreferenceStyle">@android:style/Preference.DialogPreference.YesNoPreference</item>
        <item name="dialogPreferenceStyle">@android:style/Preference.DialogPreference</item>
        <item name="editTextPreferenceStyle">@android:style/Preference.DialogPreference.EditTextPreference</item>
        <item name="ringtonePreferenceStyle">@android:style/Preference.RingtonePreference</item>
        <item name="preferenceLayoutChild">@android:layout/preference_child</item>
        <item name="preferencePanelStyle">@style/PreferencePanel</item>
        <item name="preferenceHeaderPanelStyle">@style/PreferenceHeaderPanel</item>
        <item name="preferenceListStyle">@style/PreferenceHeaderList</item>
        <item name="preferenceFragmentListStyle">@style/PreferenceFragmentList</item>
        <item name="preferenceFragmentPaddingSide">@dimen/preference_fragment_padding_side</item>
        <item name="detailsElementBackground">@android:drawable/panel_bg_holo_dark</item>
13)Search控件样式( Search Style)
<!-- Search widget styles -->
<item name="searchWidgetCorpusItemBackground">@android:color/search_widget_corpus_item_background</item>
<!-- SearchView attributes -->
<item name="searchDropdownBackground">@android:drawable/spinner_dropdown_background</item>
<item name="searchViewTextField">@drawable/textfield_searchview_holo_dark</item><item name="searchViewTextFieldRight">@drawable/textfield_searchview_right_holo_dark</item>
<item name="searchViewCloseIcon">@android:drawable/ic_clear</item>
<item name="searchViewSearchIcon">@android:drawable/ic_search</item>
<item name="searchViewGoIcon">@android:drawable/ic_go</item>
<item name="searchViewVoiceIcon">@android:drawable/ic_voice_search</item>
<item name="searchViewEditQuery">@android:drawable/ic_commit_search_api_holo_dark</item>
<item name="searchViewEditQueryBackground">?attr/selectableItemBackground</item>

<item name="searchDialogTheme">@style/Theme.SearchBar</item>
14)ActionBar样式( Action bar Style)
        <!-- Action bar styles -->
        <item name="actionDropDownStyle">@android:style/Widget.Spinner.DropDown</item>
        <item name="actionButtonStyle">@android:style/Widget.ActionButton</item>
        <item name="actionOverflowButtonStyle">@android:style/Widget.ActionButton.Overflow</item>
        <item name="actionModeBackground">@android:drawable/cab_background_top_holo_dark</item>
        <item name="actionModeSplitBackground">@null</item>
        <item name="actionModeCloseDrawable">@android:drawable/ic_menu_close_clear_cancel</item>
        <item name="actionModeCutDrawable">@android:drawable/ic_menu_cut_holo_dark</item>
        <item name="actionModeCopyDrawable">@android:drawable/ic_menu_copy_holo_dark</item>
        <item name="actionModePasteDrawable">@android:drawable/ic_menu_paste_holo_dark</item>
        <item name="actionModeSelectAllDrawable">@android:drawable/ic_menu_selectall_holo_dark</item>
        <item name="actionModeShareDrawable">@android:drawable/ic_menu_share_holo_dark</item>
        <item name="actionModeFindDrawable">@android:drawable/ic_menu_find_holo_dark</item>
        <item name="actionModeWebSearchDrawable">@android:drawable/ic_menu_search</item>
        <item name="actionBarTabStyle">@style/Widget.ActionBar.TabView</item>
        <item name="actionBarTabBarStyle">@style/Widget.ActionBar.TabBar</item>
        <item name="actionBarTabTextStyle">@style/Widget.ActionBar.TabText</item>
        <item name="actionModeStyle">@style/Widget.ActionMode</item>
        <item name="actionModeCloseButtonStyle">@style/Widget.ActionButton.CloseMode</item>
        <item name="actionBarStyle">@android:style/Widget.ActionBar</item>
        <item name="actionBarSplitStyle">?android:attr/actionBarStyle</item>
        <item name="actionBarSize">@dimen/action_bar_default_height</item>
        <item name="actionModePopupWindowStyle">?android:attr/popupWindowStyle</item>
        <item name="actionMenuTextAppearance">@android:style/TextAppearance.Holo.Widget.ActionBar.Menu</item>
        <item name="actionMenuTextColor">?android:attr/textColorPrimary</item>
        <item name="actionBarWidgetTheme">@null</item>
        <item name="actionBarDivider">?android:attr/dividerVertical</item>
        <item name="actionBarItemBackground">?android:attr/selectableItemBackground</item>

        <item name="dividerVertical">@drawable/divider_vertical_dark</item>
        <item name="dividerHorizontal">@drawable/divider_vertical_dark</item>
        <item name="buttonBarStyle">@android:style/ButtonBar</item>
        <item name="buttonBarButtonStyle">?android:attr/buttonStyle</item>
        <item name="segmentedButtonStyle">@android:style/SegmentedButton</item>
15)其它样式
        <!-- PreferenceFrameLayout attributes -->
        <item name="preferenceFrameLayoutStyle">@android:style/Widget.PreferenceFrameLayout</item>

        <!-- NumberPicker style-->
        <item name="numberPickerStyle">@style/Widget.NumberPicker</item>

        <!-- CalendarView style-->
        <item name="calendarViewStyle">@style/Widget.CalendarView</item>

        <!-- TimePicker style -->
        <item name="timePickerStyle">@style/Widget.TimePicker</item>

        <!-- DatePicker style -->
        <item name="datePickerStyle">@style/Widget.DatePicker</item>

        <item name="fastScrollThumbDrawable">@android:drawable/scrollbar_handle_accelerated_anim2</item>
        <item name="fastScrollTrackDrawable">@null</item>
        <item name="fastScrollPreviewBackgroundRight">@android:drawable/menu_submenu_background</item>
        <item name="fastScrollPreviewBackgroundLeft">@android:drawable/menu_submenu_background</item>
        <item name="fastScrollOverlayPosition">floating</item>
        <item name="fastScrollTextColor">@android:color/primary_text_dark</item>

        <!-- Pointer style -->
        <item name="pointerStyle">@android:style/Pointer</item>

        <!-- Accessibility focused drawable. -->
        <item name="accessibilityFocusedDrawable">@android:drawable/view_accessibility_focused</item>
5、一些细化的style(特别是大量的控件的样式)定义在了styles.xml

themes.xml偏全局,styles.xml偏细化。
我直接贴出链接地址,大家查看:
https://github.com/CyanogenMod/android_frameworks_base/blob/cm-11.0/core/res/res/values/styles.xml

6、我们能做什么

两点说明:

  • 我们可以根据这些样式改变系统控件的样式
  • 但不是所有的样式我们都可以修改,比如AlertDialog,布局和一些属性都是写在硬编码中了,我们是无法修改的。

通过学习这些主题和样式,我们能准确的把握我们能定制哪些样式,哪些我们不能定制,是否需要重新写一套替代的UI控件。

这个在项目决策和进度评估上是非常重要的。

7、简单例子

以CheckBox为例,CheckBox使用的样式我们猜测使用checkboxStyle(实际上从CheckBox源码分析看,它确实使用的是checkboxStyle):

<item name="checkboxStyle">@android:style/Widget.CompoundButton.CheckBox</item>

查看styles.xml中的Widget.CompoundButton.CheckBox的定义:

<style name="Widget.CompoundButton.CheckBox">
    <item name="android:button">?android:attr/listChoiceIndicatorMultiple</item>
</style>

继续在themes.xml中查看listChoiceIndicatorMultiple的定义,不同的主题可能定义会有不同,我们选取最基本的那个:

    <item name="listChoiceIndicatorMultiple">@android:drawable/btn_check</item>

CheckBox的样式就是这个btn_check:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Enabled states -->

    <item android:state_checked="true" android:state_window_focused="false"
          android:state_enabled="true"
          android:drawable="@drawable/btn_check_on" />
    <item android:state_checked="false" android:state_window_focused="false"
          android:state_enabled="true"
          android:drawable="@drawable/btn_check_off" />

    <item android:state_checked="true" android:state_pressed="true"
          android:state_enabled="true"
          android:drawable="@drawable/btn_check_on_pressed" />
    <item android:state_checked="false" android:state_pressed="true"
          android:state_enabled="true"
          android:drawable="@drawable/btn_check_off_pressed" />

    <item android:state_checked="true" android:state_focused="true"
          android:state_enabled="true"
          android:drawable="@drawable/btn_check_on_selected" />
    <item android:state_checked="false" android:state_focused="true"
          android:state_enabled="true"
          android:drawable="@drawable/btn_check_off_selected" />
    <item android:state_checked="false"
          android:state_enabled="true"
          android:drawable="@drawable/btn_check_off" />
    <item android:state_checked="true"
          android:state_enabled="true"
          android:drawable="@drawable/btn_check_on" />

    <!-- Disabled states -->

    <item android:state_checked="true" android:state_window_focused="false"
          android:drawable="@drawable/btn_check_on_disable" />
    <item android:state_checked="false" android:state_window_focused="false"
          android:drawable="@drawable/btn_check_off_disable" />

    <!-- 这两张图片暂时没有,后面自定义的例子我把这两个注释掉
    <item android:state_checked="true" android:state_focused="true"
        android:drawable="@drawable/btn_check_on_disable_focused" />
    <item android:state_checked="false" android:state_focused="true"
        android:drawable="@drawable/btn_check_off_disable_focused" />
    -->

    <item android:state_checked="false" android:drawable="@drawable/btn_check_off_disable" />
    <item android:state_checked="true" android:drawable="@drawable/btn_check_on_disable" />

</selector>

我把这个btn_check.xml拷贝到Android工程的drawable目录下,同时把一些新的checkbox的图片也准备好,拷贝到drawable-hdpi目录下:

同时在工程的styles.xml覆盖checkBox style:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="android:checkboxStyle">@style/CustomeCheckBox</item>
    </style>

    <style name="CustomeCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox">
        <item name="android:button">@drawable/btn_check</item>
    </style>
</resources>

我截取2个不同的状态的效果图如下,

8、小结

本文纯从主题和样式对系统样式,特别是控件样式笼统的描述了一遍。

如果结合系统源码和主题样式一起学习,可以更深入的了解android主题机制。