樣式(Style)和主題(Theme)
開頭語:在android很多地方要對控件的樣式(如:ActionBar的樣式)和Activity的樣式進行設置,對應控件的樣式設置是(Style)對Activity的樣式設置是(Theme)
API是這樣來解釋Style的:
A style resource defines the format and look for a UI.A style can be applied to an individual View
(from within a layout file)
or to an entire Activity
or application (from within the manifest file)
下面就來說說怎么進行style設置:
1.在資源文件夾里創建好style.xml和theme.xml
2.在XML文件中都要添加一個<resources>根節點
3.在根節點里面<style>增加一個全局唯一的名字,也可以選擇增加一個父類屬性,繼承已有的style
通過兩個例子來說明Style和Theme
Style:
在Values的Style.xml中寫:
1 <!-- TextView設定的簡單樣式 --> 2 <style name="TextStyle"> 3 <item name="android:textColor">#008</item> 4 <item name="android:textSize">18sp</item> 5 </style>
在MainActivity當中添加一個TextView
1 <TextView 2 style="@style/TextStyle" 3 android:layout_width="wrap_content" 4 android:layout_height="wrap_content" 5 android:text="我是沒有改變style的TextView" />
在沒有寫style="@style/TextStyle"的時候運行出來TextView的字體就是普通效果:
再加了style="@style/TextStyle"引用style.xml定義的樣式后TextView顯示的字體變成了:
則對View的樣式添加成功了的
Theme:
現在Values.中添加一個Theme.xml文件,在Theme.xml的<Resource>根節點里面寫上<Style>並定義一些樣式,在ANDROID的SDK中已經定義了很多Theme。
Theme是針對窗體級別的,改變窗體樣式,如果你想對應用的所有Activity的樣式進行設置就要在AndroidManifest.xml中對<Application>中添加android:theme屬性
如:
<Application android:theme="@style/AppTheme"></Application>即可
若想修改指定Activity就在AndroidManifest.xml對指定Activity中添加android:theme屬性
如:
<activity android:theme="@style/AppTheme" ></activity>
Android SDK定義好的一些常用theme:
下面的前三個之外直接復制就會出錯。@是說明系統已經定義過的,@android:style/ 是必須帶上的。
?android:theme="@android:style/Theme.Dialog"
將一個Activity顯示為對話框模式
?android:theme="@android:style/Theme.NoTitleBar" 不顯示應用程序標題欄
?android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 不顯示應用程序標題欄,並全屏
?android:theme="Theme.Light" 背景為白色
?android:theme="Theme.Light.NoTitleBar" 白色背景並無標題欄
?android:theme="Theme.Light.NoTitleBar.Fullscreen" 白色背景,無標題欄,全屏
?android:theme="Theme.Black" 背景黑色
?android:theme="Theme.Black.NoTitleBar" 黑色背景並無標題欄
?android:theme="Theme.Black.NoTitleBar.Fullscreen" 黑色背景,無標題欄,全屏
?android:theme="Theme.Wallpaper" 用系統桌面為應用程序背景
?android:theme="Theme.Wallpaper.NoTitleBar" 用系統桌面為應用程序背景,且無標題欄
?android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen" 用系統桌面為應用程序背景,無標題欄,全屏
?android:theme="Translucent"
?android:theme="Theme.Translucent.NoTitleBar" 半透明,無標題
?android:theme="Theme.Translucent.NoTitleBar.Fullscreen" 半透明,無標題,全屏
?android:theme="Theme.Panel" 面板風格顯示
?android:theme="Theme.Light.Panel" 平板風格顯示