【基于最新的Android4.4的源码分析】
每家公司或者每个移动团队无不想开发出一套自己的UI框架,融入自己的设计和特性,这必然会去修改android的ui。
所以,学习和理解android的UI设计是最基础和非常有必要的。android ui设计最重要的就是主题和样式。1、位置
在Android的frameworks/base/core/res/res/values目录下有一下几个文件:themes.xmlthemes_device_defaults.xmlstyles.xmlstyles_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厂商)。系统如何去选择默认的主题呢?/**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的历史:public static class VERSION_CODES { /** * Magic version number for a current development build, which has * not yet turned into an official release. */ public static final int CUR_DEVELOPMENT = 10000; /** * October 2008: The original, first, version of Android. Yay! */ public static final int BASE = 1; /** * February 2009: First Android update, officially called 1.1. */ public static final int BASE_1_1 = 2; /** * May 2009: Android 1.5. */ public static final int CUPCAKE = 3; /** * September 2009: Android 1.6. */ public static final int DONUT = 4; /** * November 2009: Android 2.0 */ public static final int ECLAIR = 5; /** * December 2009: Android 2.0.1 */ public static final int ECLAIR_0_1 = 6; /** * January 2010: Android 2.1 */ public static final int ECLAIR_MR1 = 7; /** * June 2010: Android 2.2 */ public static final int FROYO = 8; /** * November 2010: Android 2.3 */ public static final int GINGERBREAD = 9; /** * February 2011: Android 2.3.3. */ public static final int GINGERBREAD_MR1 = 10; /** * February 2011: Android 3.0. */ public static final int HONEYCOMB = 11; /** * May 2011: Android 3.1. */ public static final int HONEYCOMB_MR1 = 12; /** * June 2011: Android 3.2. */ public static final int HONEYCOMB_MR2 = 13; /** * October 2011: Android 4.0. */ public static final int ICE_CREAM_SANDWICH = 14; /** * December 2011: Android 4.0.3. */ public static final int ICE_CREAM_SANDWICH_MR1 = 15; /** * June 2012: Android 4.1. */ public static final int JELLY_BEAN = 16; /** * Android 4.2: Moar jelly beans! */ public static final int JELLY_BEAN_MR1 = 17; /** * Android 4.3: Jelly Bean MR2, the revenge of the beans. */ public static final int JELLY_BEAN_MR2 = 18; /** * Android 4.4: KitKat, another tasty treat. */ public static final int KITKAT = 19; }
3、系统主题Theme列表
系统默认大的主题是三种:Theme,Theme.Holo,Theme.DeviceDefault, 但是实际上在此基础系统还定义了大量的派生主题,最典型的是对应的Light主题。除此之外,还有很多,在此一一列出,打字太痛苦了,我贴出截图:了解android系统定义的主题之后,我们就可以根据实际情况在自己的应用中使用这些主题,但是如果想修改主题的某些内容,需要进一步深入。4、详解每个主题中定义item分类
一个完整的主题应该定义哪些内容呢,以Theme为例,如下:1)颜色- @android:color/bright_foreground_dark
- @android:color/bright_foreground_dark_inverse
- @android:color/background_dark
- ?android:attr/colorBackground
- @color/legacy_pressed_highlight
- @color/legacy_long_pressed_highlight
- @color/legacy_selected_highlight
- @color/legacy_selected_highlight
- @color/legacy_selected_highlight
2)字体
- @android:style/TextAppearance
- @android:style/TextAppearance.Inverse
- @android:color/primary_text_dark
- @android:color/secondary_text_dark
- @android:color/tertiary_text_dark
- @android:color/primary_text_light
- @android:color/secondary_text_light
- @android:color/tertiary_text_light
- @android:color/primary_text_dark_disable_only
- @android:color/primary_text_light_disable_only
- @android:color/primary_text_dark_nodisable
- @android:color/secondary_text_dark_nodisable
- @android:color/primary_text_light_nodisable
- @android:color/secondary_text_light_nodisable
- @android:color/hint_foreground_dark
- @android:color/hint_foreground_light
- @android:color/search_url_text
- @android:color/highlighted_text_dark
- @android:color/highlighted_text_light
- @android:color/link_text_dark
- @android:color/link_text_light
- @android:color/primary_text_light_disable_only
- @android:style/TextAppearance.Large
- @android:style/TextAppearance.Medium
- @android:style/TextAppearance.Small
- @android:style/TextAppearance.Large.Inverse
- @android:style/TextAppearance.Medium.Inverse
- @android:style/TextAppearance.Small.Inverse
- @android:style/TextAppearance.SearchResult.Title
- @android:style/TextAppearance.SearchResult.Subtitle
- @android:style/TextAppearance.EasyCorrectSuggestion
- @android:style/TextAppearance.MisspelledSuggestion
- @android:style/TextAppearance.AutoCorrectionSuggestion
- @android:style/TextAppearance.Widget.Button
- @android:color/primary_text_light
- @android:drawable/edit_text
- @android:string/candidates_style
- @android:drawable/indicator_check_mark_dark
- @android:drawable/indicator_check_mark_light
- @android:style/TextAppearance.Widget.PopupMenu.Large
- @android:style/TextAppearance.Widget.PopupMenu.Small
3)按钮
- @android:style/Widget.Button
- @android:style/Widget.Button.Small
- @android:style/Widget.Button.Inset
- @android:style/Widget.Button.Toggle
- @android:drawable/item_background
- ?android:attr/buttonStyle
- @android:drawable/ic_ab_back_holo_dark
4)List
- 64dip
- ?android:attr/listPreferredItemHeight
- ?android:attr/listPreferredItemHeight
- ?android:attr/listPreferredItemHeight
- ?android:attr/textAppearanceLarge
- ?android:attr/textAppearanceLarge
- 6dip
- 6dip
- 6dip
- 6dip
5)Window
- @android:drawable/screen_background_selector_dark
- @null
- false
- false
- false
- false
- @null
- false
- @android:style/WindowTitle
- 25dip
- @android:style/WindowTitleBackground
- @android:style/Animation.Activity
- stateUnspecified|adjustUnspecified
- false
- false
- false
- false
- false
6)Dialog
- @android:style/Theme.Dialog
- @layout/dialog_title_icons
- @layout/dialog_custom_title
- @layout/dialog_title
7)AlertDialog
- @android:style/Theme.Dialog.Alert
- @android:style/AlertDialog
- true
- @android:drawable/ic_dialog_alert
8)Panel
- @android:drawable/menu_background
- @android:drawable/menu_background_fill_parent_width
- #000
- ?android:attr/textColorPrimary
- ?android:attr/textAppearance
- false
- 296dip
9)滚动条(Scrollbar)
- 250
- 300
- 10dip
- @android:drawable/scrollbar_handle_horizontal
- @android:drawable/scrollbar_handle_vertical
- @null
- @null
10)文字选中(Text selection)
- @android:drawable/text_select_handle_left
- @android:drawable/text_select_handle_right
- @android:drawable/text_select_handle_middle
- @android:style/Widget.TextSelectHandle
- @android:layout/text_edit_paste_window
- @android:layout/text_edit_no_paste_window
- @android:layout/text_edit_side_paste_window
- @android:layout/text_edit_side_no_paste_window
- @android:style/Widget.TextSuggestionsPopupWindow
- @android:layout/text_edit_suggestion_item
- @null
有点长,下篇继续列举,并深入分析具体style.