例子
<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>
<TextView
style="@style/CodeFont"
android:text="@string/hello" />
style作用
- 設計與內容分開
- 可繼承
- 便于統一風格
書寫方式
- 關于繼承系統的書寫
<style name="CodeFont" parent="@android:style/TextAppearance.Medium">
...
</style>
- 關于繼承自定義的書寫
<style name="CodeFont1" parent="CodeFont">
...
</style>
- 關于繼承系統theme的書寫
<style name="LightThemeSelector" parent="android:Theme.Holo.Light">
...
</style>
<style name="LightThemeSelector" parent="@android:style/Theme.Holo.Light">
...
</style>
- 關于繼承自定義theme的書寫
<style name="LightThemeSelector" parent="@style/Theme.AppCompat">
...
</style>
style與theme的區別
Theme是針對窗體級別的,改變窗體樣式;
Style是針對窗體元素級別的,改變指定控件或者Layout的樣式
簡單的說就是Theme里面有包含了好多好多Style
android:theme與app:popupTheme
- android:theme設置是View和子View的主題(API20+)
- app:popupTheme設置的是該view節點下的view的theme(通俗的說就是類似css選擇器的作用)
常用于Toolbar下
比如
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/AppTheme.AppBarOverlay"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
android:theme="@style/AppTheme.AppBarOverlay設置了Toolbar的主題
app:popupTheme="@style/AppTheme.PopupOverlay"設置了Toolbar節點下的view的主題
默認情況下我們使用theme為android:Theme.Holo.Light.DarkActionBar那么ActionBar文字是白的,ActionBar Overflow彈出的是黑底白字,如果需求是白底黑字那么只要設置toolbar的app:popupTheme="ThemeOverlay.AppCompat.Light"
常用的自帶Theme
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=”Theme.Translucent : 透明背景
android:theme=”Theme.Translucent.NoTitleBar” : 透明背景并無標題
android:theme=”Theme.Translucent.NoTitleBar.Fullscreen” : 透明背景并無標題,全屏
android:theme=”Theme.Panel “: 面板風格顯示
android:theme=”Theme.Light.Panel” : 平板風格顯示
常用的Theme的屬性
名稱 | 作用 |
---|---|
android:windowIsTranslucent | 設置透明屬性(防止啟動時候的閃屏) |
android:windowBackground | 設置背景圖片 |
android:windowAnimationStyle | Activity進入退出動畫 |
android:windowNoTitle | 不顯示標題欄 |
android:textColor | 默認字體顏色 |
android:windowFullscreen | 是否全屏 |
android:windowIsFloating | 是否浮現在activity之上 |
android:backgroundDimEnabled | 背景是否模糊顯示 |
常用的style元素屬性
非官方
附上一張官方的
官方