1、什么是Style,什么是Theme?
Style 和 theme:是一個包含一種 或者 多種格式化 屬性 的集合 ,并且 style和theme都是資源,存放在res/values/styles.xml 文件夾下 即可,style和theme的定義格式相同,在res.values中建立一個styles.xml,然后創(chuàng)建節(jié)點resources-style-item。 不過style是針對view來說的,比如 TextView,EditText這些,而Theme必須針對整個activity或者 整個application,你必須在AndroidManifest.xml中 的或者中定義。
先來看看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。parent后面就是父類的style,CodeFont繼承這個父類的屬性.可以看到這個父類的style是android中默認的,你也可以繼承你自定義的style,這時候不需要再寫 parent屬性,而是使用ContFont.red這樣的方式,而且你可以繼續(xù)繼承,寫成ContFont.red.small。 接下來 每一個item定義一個屬性。定義屬性的最好方法就是在api文檔里找到這個view的xml屬性,比如在EditText中有InputType 這個屬性,那么在你的style里面你就可以來定義它。
這樣一個style就寫好了。
使用也非常簡單,我們只要在寫我們的view時,加入style標簽就可以了,就像這樣
<TextView style="@style/CodeFont" android:text="@string/hello" />下面講講主題,主題需要在AndroidManifest.xml中注冊。如果你想整個程序都使用這個主題,你可以這樣寫
<application android:theme="@style/CustomTheme">如果你只需要在某個Activity中使用主題,那么只要在Activity標簽中寫入android:theme= 就可以了,android有很多好的默認主題,比如
<activity android:theme="@android:style/Theme.Dialog">這就會使你的整個Activity變成一個對話框形式。 或者,如果你希望背景是透明的,可以這樣寫
<activity android:theme="@android:style/Theme.Translucent">同樣的我們也可以繼承父類theme,寫法和style一樣。你也可以自己定義一個theme,寫個例子
<?xml version="1.0" encoding="utf-8"?><resources> <style name="CustomTheme"> <item name="android:windowNoTitle">true</item> <item name="windowFrame">@drawable/screen_frame</item> <item name="windowBackground">@drawable/screen_background_white</item> <item name="panelForegroundColor">#FF000000</item> <item name="panelBackgroundColor">#FFFFFFFF</item> <item name="panelTextColor">?panelForegroundColor</item> <item name="panelTextSize">14</item> <item name="menuItemTextColor">?panelTextColor</item> <item name="menuItemTextSize">?panelTextSize</item> </style></resources>如果你要在Java代碼中加載主題的話,只要用setTheme(R.style.CustomTheme)就可以了,不過記得一定要在初始化任何view之前,比如一定要放在我們常用的setContentView()之前。通常,不建議這么做。
Android系統(tǒng)的 themes.xml 和 style.xml (位于/base/core/res/res/values/) 包含了很多系統(tǒng)定義好的style,建議在里面挑個合適的,然后再繼承修改。
下邊是SDK中主題的一個例子:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="CustomTheme"> <item name="android:windowNoTitle">true</item> <item name="windowFrame">@drawable/screen_frame</item> <item name="windowBackground">@drawable/screen_background_white</item> <item name="panelForegroundColor">#FF000000</item> <item name="panelBackgroundColor">#FFFFFFFF</item> <item name="panelTextColor">?panelForegroundColor</item> <item name="panelTextSize">14</item> <item name="menuItemTextColor">?panelTextColor</item> <item name="menuItemTextSize">?panelTextSize</item> </style> </resources>注意:我們用了@符號和?符號來應用資源。 @符號 表明 我們引用的資源是前邊定義過的(或者在前一個項目中或者在Android 框架中)。 問號?表明 我們引用的資源的值在 當前的 主題當中定義過。 通過引用在里邊定義的名字 可以做到(panelTextColor 用的顏色 和 panelForegroundColor中定義的一樣 )。這中技巧只能用在XML資源當中
在程序中 使用主題的方法:
PRotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTheme(android.R.style.Theme_Light); setContentView(R.layout.linear_layout_3); }如果你喜歡一個主題,但是想做一些輕微的改變,你只需要將這個主題添加為父主題。比如我們修改Theme.Dialog主題。我們來繼承Theme.Dialog來生成一個新的主題。
<style name=”CustomDialogTheme” parent=”@android:style/Theme.Dialog” >繼承了Theme.Dialog后,我們可以按照我們的要求來調(diào)整主題。我們可以修改在Theme.Dialog中定義的每個item元素的值,然后我們在Android Manifest 文件中使用CustomDialogTheme 而不是 Theme.Dialog 。
Android中自定義樣式與View的構(gòu)造函數(shù)中的第三個參數(shù)defStyle的意義
Android 風格與主題(style and theme)
Android 樣式和主題(style & theme)
新聞熱點
疑難解答