標籤:
User Interface(以下簡稱UI)是任何可以向使用者展示、與使用者互動的圖形介面。Android提供了大量預定義的UI組件( a variety of pre-built UI components),比如Layout資源,除此之外,Android還提供了特殊的UI模型,如dialogs、notifications、menus 等。
從本文起,將介紹Android的各種UI資源以及如何自訂UI資源。本文將介紹:
您還可以參考這些部落格文章:
《Say Goodbye to the Menu Button》;
《New Layout Widgets: Space and GridLayout》;
《Customizing the Action Bar》;
《Horizontal View Swiping with ViewPager》。
或者這些Training:
《Implementing Effective Navigation》;
《Designing for Multiple Screens》;
《Improving Layout Performance》。
樣式和主題(Styles and Themes)
一個Style資源是一組屬性的集合,用於指定一個View或一個Window的樣子和格式(A style is a collection of properties that specify the look and format for a View or window. A style can specify properties ),比如高度、內邊距、字型顏色、字型大小、背景顏色 等( such as height, padding, font color, font size, background color, and much more)。Style資源在XML資源中定義,以區分layout資源。
具有相同或相似外觀的UI控制項可以共用同一個Style資源,如下所示:
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#00FF00" android:typeface="monospace" android:text="@string/hello" />
將上述TextView中某些屬性抽取出來,定義在style資源中,並給該資源起名為CodeFont,這樣其他TextView也可以應用該資源,這些TextView的風格將保持一致。
<TextView style="@style/CodeFont" android:text="@string/hello" />
theme 資源是一種特殊的style資源,只不過theme資源作用於整個Activity或Application上,而不是View或Window上(A theme is a style applied to an entire Activity or application, rather than an individual View)。將一個style作為theme資源作用在Activity或Application上,實際上是作用在了activity或application中的所有控制項上。比如說,若將style資源CodeFont作為theme資源作用在了 Activity上,那麼這個activity中的所有控制項都將具有綠色(#00FF00)以及等寬字型(monospace font)。
定義樣式(Defining Styles)
style應定義在res/values/目錄下的XML檔案中,檔案名稱任意(The name of the XML file is arbitrary)。在檔案中<resources>必須作為style資源的根標籤。根標籤內部包含您需要自訂的style資源,以<style>標籤包含。其中<style>標籤中的屬性name是不可預設的,它指定了該style資源的名字,如程式中需要引用該資源,那麼引用的就是name中的內容。每一個<item>標籤指定了style的一個屬性,以下是一個自訂的style樣本:
<?xml version="1.0" encoding="utf-8"?><resources> <style name="CodeFont" parent="@android:style/TextAppearance.Medium"> <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">wrap_content</item> <item name="android:textColor">#00FF00</item> <item name="android:typeface">monospace</item> </style></resources>
您可以在其他位置以@style/CodeFont的形式引用該style資源。<style>標籤的parent 屬性不是必須指定的,它的值是一個資源ID,引用了一個其他的style,這表示現在這個正在自訂的style繼承了這個引用style的所有屬性。您也可以在自己的style中覆蓋需要替換的父style屬性。
如果需要將一個style應用在一個activity或application上,那個這個style就變成了theme,它的定義與作用在view上的style沒有任何區別( exactly the same as a style for a View),只是叫法不一樣罷了。
繼承(Inheritance)
通過<parent>屬性,可以方便地繼承一個現成的style資源,您可以在此基礎上添加或修改某些屬性。Android提供了大量的style資源,比如,系統提供了裝飾字型的style資源:TextAppearance,您可以直接繼承它,並修改它:
<style name="GreenText" parent="@android:style/TextAppearance"> <item name="android:textColor">#00FF00</item> </style>
如果您需要繼承自訂的style資源,那麼無需使用<parent>屬性。只需要在新定義的style資源的name屬性中將被繼承的style資源名作為首碼,並以” . “分隔即可:
<style name="CodeFont.Red"> <item name="android:textColor">#FF0000</item> </style>
例如,現在要繼承自訂的CodeFont style,新的自訂style名字為Red,那麼只需命名為:CodeFont.Red即可。並在其他位置以@style/CodeFont.Red的方式引用。
如果需要繼續繼承上述style,那麼name也只需以鏈式的方式書寫即可:
<style name="CodeFont.Red.Big"> <item name="android:textSize">30sp</item> </style>
!請注意:這種鏈式地style繼承方式只適用於繼承您自訂的style資源,如需繼承Android內建的style資源,仍需使用<parent>屬性。
樣式屬性(Style Properties)
<item>標籤用於指定自訂的屬性。您可以指定類似於layout_width、textColor這樣的屬性。可以通過具體的View所具有的屬性來定義。在代碼中可以通過 R.attr來擷取這些屬性值。如果您引用的style中包含了不支援該控制項的屬性,那麼這些屬性將被忽略。
某些特殊的屬性不被任何View支援,只能應用在theme中。也就是說,這些屬性只被Activity或Application支援。比如有的屬性用於確定是否隱藏應用的標題、是否隱藏狀態列或者修改window的背景色 等。這些屬性並不屬於任何View,它們有一個共同的特點,就是名字以window開頭。比如:windowNoTitle 和 windowBackground 屬性。
在UI中應用樣式和主題(Applying Styles and Themes to the UI)
應用style的方式有兩種:
!請注意:將style資源應用在某個View上,它僅對該View的樣式生效,也就是說,如果您將style資源應用在ViewGroup上,那麼該資源不會自動地應用在ViewGroup的子View上,您只能將每一個View分別引用該style資源,或者乾脆直接在ViewGroup的Activity中將該style作為theme引用。另外需要注意的是,style屬性是不包含“android”首碼的
style應用在View上(Apply a style to a View)
樣本如下:
<TextView style="@style/CodeFont" android:text="@string/hello" />
theme 應用在Activity 或 application上(Apply a theme to an Activity or application)
樣本如下:(在AndroidManifest.xml 中)
<application android:theme="@style/CustomTheme">
如需將Activity定義成對話方塊的樣子,那麼可以引用Theme.Dialog主題:
<activity android:theme="@android:style/Theme.Dialog">
如需使Activity的背景透明,那麼可以引用Theme.Translucent主題:
<activity android:theme="@android:style/Theme.Translucent">
如需定製系統主題,那麼只需像style的繼承那樣定製即可:
<color name="custom_theme_color">#b0b0ff</color><style name="CustomTheme" parent="android:Theme.Light"> <item name="android:windowBackground">@color/custom_theme_color</item> <item name="android:colorBackground">@color/custom_theme_color</item></style>
!請注意:由於android:windowBackground屬性的值只支援參考型別,並不接受字面值,所以寫法如上所示。
下面只需將該定製的主題應用到activity上即可:
<activity android:theme="@style/CustomTheme">
為整個應用選擇一個基礎主題(Select a theme based on platform version)
新的Android版本增加了一些系統主題,為了讓這些主題也能應用在舊的Android版本中,應當在目錄中使用不同尾碼的檔案夾做以區分,比如:android:Theme.Holo.Light是在Android 3.0之後新增的主題,那麼您需要定製一個繼承了該主題的theme,並把該主題放在res/values-v11/styles.xml中:
<style name="LightThemeSelector" parent="android:Theme.Holo.Light"> ...</style>
再在預設的目錄中( res/values/styles.xml)定義如下主題:
<style name="LightThemeSelector" parent="android:Theme.Light"> ...</style>
因為android:Theme.Light主題在Android 3.0之前也支援,所以當裝置版本小於3.0時,將載入該主題。
通過R.styleable.Theme可擷取該主題的所有屬性集合。
Android官方文檔之User Interface(Styles and Themes)