第十四章:樣式(Style)和主題(Theme)

簡介html

Android的樣式(Style)和主題(Theme)文件就比如WEB開發中的CSS同樣,能夠實現UI界面的風格統一管理,這和Windows平臺的XAML格式(Silverlight、WPF)相似。好比咱們遇到特殊的節日咱們只需變動咱們的Style和Theme就能夠切換一種新的Style和Theme。還有現有的一些應用提供咱們能夠自定義UI風格,就是應用的這個原理。Android的主題樣式文件存儲在res\values目錄下,如res\values\styles.xml。android

樣式(Style)app

Style咱們能夠應用到單個組件或者一類組件,好比咱們能夠設置組件的字體、顏色等。less

下面的文件就是Android SDK(Version16)提供的一個Style文件片斷(詳細文件在:\android-sdk\platforms\android-16\data\res\values\styles.xml),以下所示咱們能夠看到系統的各個控件的默認的樣式風格ide

<?xml version="1.0" encoding="utf-8"?> <resources> <!-- Global Theme Styles --> <eat-comment /> <style name="WindowTitleBackground"> <item name="android:background">@android:drawable/title_bar</item> </style> <style name="WindowTitle"> <item name="android:singleLine">true</item> <item name="android:textAppearance">@style/TextAppearance.WindowTitle</item> <item name="android:shadowColor">#BB000000</item> <item name="android:shadowRadius">2.75</item> </style> <style name="Widget.EditText"> <item name="android:focusable">true</item> <item name="android:focusableInTouchMode">true</item> <item name="android:clickable">true</item> <item name="android:background">?android:attr/editTextBackground</item> <item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item> <item name="android:textColor">?android:attr/editTextColor</item> <item name="android:gravity">center_vertical</item> </style> <style name="Widget.ExpandableListView" parent="Widget.ListView"> <item name="android:groupIndicator">@android:drawable/expander_group</item> <item name="android:indicatorLeft">?android:attr/expandableListPreferredItemIndicatorLeft</item> <item name="android:indicatorRight">?android:attr/expandableListPreferredItemIndicatorRight</item> <item name="android:childDivider">@android:drawable/divider_horizontal_dark_opaque</item> </style> <style name="Widget.ImageButton"> <item name="android:focusable">true</item> <item name="android:clickable">true</item> <item name="android:scaleType">center</item> <item name="android:background">@android:drawable/btn_default</item> </style> <style name="Widget.WebView"> <item name="android:focusable">true</item> <item name="android:focusableInTouchMode">true</item> <item name="android:scrollbars">horizontal|vertical</item> </style> <style name="Widget.Gallery"> <item name="android:fadingEdge">none</item> <item name="android:gravity">center_vertical</item> <item name="android:spacing">-20dip</item> <item name="android:unselectedAlpha">0.85</item> </style> <style name="Widget.PopupWindow"> <item name="android:popupBackground">@android:drawable/editbox_dropdown_background_dark</item> <item name="android:popupAnimationStyle">@android:style/Animation.PopupWindow</item> </style> <!-- Default

咱們能夠看到Style文件的跟元素像其餘資源文件同樣是:<resources> ,樣式文件內部由多個的<style>節點構成,每個style構成一個樣式,樣式還能夠繼承。以下所示,指定了樣式名稱已經其父樣式的名稱:字體

<style name="Widget.ExpandableListView" parent="Widget.ListView"> 

固然若是子樣式定義的屬性在父樣式中存在,那麼子樣式將會覆蓋父樣式。spa

好比咱們能夠定義以下的樣式:code

<?xml version="1.0" encoding="UTF-8"?> <resources> <!-- 定義一個樣式,指定字體大小、字體顏色 --> <style name="style1"> <item name="android:textSize">20sp</item> <item name="android:textColor">#00d</item> </style> <!-- 定義一個樣式,繼承前一個顏色 --> <style name="style2" parent="@style/style1"> <item name="android:background">#ee6</item> <item name="android:padding">8dp</item> <!-- 覆蓋父樣式中指定的屬性 --> <item name="android:textColor">#000</item> </style> </resources>

咱們能夠在咱們的layout文件中以下使用:[packagename.]style.file_nameorm

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- 指定使用style1的樣式 --> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/style1" style="@style/style1" /> <!-- 指定使用style2的樣式 --> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/style2" style="@style/style2" /> </LinearLayout> 

主題(Theme)xml

Theme通常用於整個應用或者一些特定的Activity。Theme通常用來設置應用的或者窗體的外觀樣式,好比應用背景,應用邊框及標題等。

下面的代碼片斷是Android SDK自帶的默認主題(詳細文件在:\android-sdk\platforms\android-16\data\res\values\themes_device_defaults.xml):

<?xml version="1.0" encoding="UTF-8"?> <style name="Theme.DeviceDefault" parent="Theme.Holo" > <!-- Text styles --> <item name="textAppearance">@android:style/TextAppearance.DeviceDefault</item> <item name="textAppearanceInverse">@android:style/TextAppearance.DeviceDefault.Inverse</item> <item name="textAppearanceLarge">@android:style/TextAppearance.DeviceDefault.Large</item> <item name="textAppearanceMedium">@android:style/TextAppearance.DeviceDefault.Medium</item> <item name="textAppearanceSmall">@android:style/TextAppearance.DeviceDefault.Small</item> <item name="textAppearanceLargeInverse">@android:style/TextAppearance.DeviceDefault.Large.Inverse</item> <item name="textAppearanceMediumInverse">@android:style/TextAppearance.DeviceDefault.Medium.Inverse</item> <item name="textAppearanceSmallInverse">@android:style/TextAppearance.DeviceDefault.Small.Inverse</item> <item name="textAppearanceSearchResultTitle">@android:style/TextAppearance.DeviceDefault.SearchResult.Title</item> <item name="textAppearanceSearchResultSubtitle">@android:style/TextAppearance.DeviceDefault.SearchResult.Subtitle</item> <item name="textAppearanceButton">@android:style/TextAppearance.DeviceDefault.Widget.Button</item> <item name="textAppearanceLargePopupMenu">@android:style/TextAppearance.DeviceDefault.Widget.PopupMenu.Large</item> <item name="textAppearanceSmallPopupMenu">@android:style/TextAppearance.DeviceDefault.Widget.PopupMenu.Small</item> <!-- Button styles --> <item name="buttonStyle">@android:style/Widget.DeviceDefault.Button</item> <item name="buttonStyleSmall">@android:style/Widget.DeviceDefault.Button.Small</item> <item name="buttonStyleInset">@android:style/Widget.DeviceDefault.Button.Inset</item> <item name="buttonStyleToggle">@android:style/Widget.DeviceDefault.Button.Toggle</item> <item name="switchStyle">@android:style/Widget.DeviceDefault.CompoundButton.Switch</item> <item name="borderlessButtonStyle">@android:style/Widget.DeviceDefault.Button.Borderless</item> <item name="listSeparatorTextViewStyle">@android:style/Widget.DeviceDefault.TextView.ListSeparator</item> <!-- Window attributes --> <item name="windowTitleStyle">@android:style/WindowTitle.DeviceDefault</item> <item name="windowTitleBackgroundStyle">@android:style/WindowTitleBackground.DeviceDefault</item> <item name="android:windowAnimationStyle">@android:style/Animation.DeviceDefault.Activity</item> <!-- Dialog attributes --> <item name="alertDialogStyle">@android:style/AlertDialog.DeviceDefault</item> <item name="dialogTheme">@android:style/Theme.DeviceDefault.Dialog</item> <item name="alertDialogTheme">@android:style/Theme.DeviceDefault.Dialog.Alert</item></style> 

主題和樣式的定義方式相似:經過Style來指定而且也支持繼承。咱們自定義一個主題以下:

<?xml version="1.0" encoding="UTF-8"?> <resources> <style name="CustomrTheme"> <item name="android:windowNoTitle">true</item> <item name="android:windowFullscreen">true</item> <item name="android:windowFrame">@drawable/window_border</item> <item name="android:windowBackground">@drawable/star</item> </style> </resources>

咱們能夠在代碼中來設置一個Activity的主題,以下粗體代碼行。

public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);  setTheme(R.style.CustomerTheme) } }

咱們經過應用的的配置文件(AndroidManifest.xml)中來指定主題,而不是經過代碼行來指定。

若是要指定一個主題應用與整個App,咱們能夠設置Application節點的android:theme屬性:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.jeriffe.app"android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"  android:theme="@style/CustomerTheme"> <activity android:name=".XXXXX"> </activity> </application> </manifest> 

若是咱們想指定主題應用與具體的Activity咱們只須要設置activity的android:theme屬性:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.jeriffe.app" android:versionCode="1"android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".XXXX" android:theme="@style/CustomerTheme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 

結束語

咱們簡單的介紹了Style和Theme,若是您有WEB開發或者WPhone或者WPF開發經驗,那麼您會很好的理解Style和Theme。

相關文章
相關標籤/搜索