Android 的style和theme

例子

<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作用

  • 設(shè)計與內(nèi)容分開
  • 可繼承
  • 便于統(tǒng)一風(fēng)格

書寫方式

  • 關(guān)于繼承系統(tǒng)的書寫
<style name="CodeFont" parent="@android:style/TextAppearance.Medium">
...
</style>
  • 關(guān)于繼承自定義的書寫
<style name="CodeFont1" parent="CodeFont">
...
</style>

  • 關(guān)于繼承系統(tǒng)theme的書寫
<style name="LightThemeSelector" parent="android:Theme.Holo.Light">
...
</style>
<style name="LightThemeSelector" parent="@android:style/Theme.Holo.Light">
...
</style>
  • 關(guān)于繼承自定義theme的書寫
<style name="LightThemeSelector" parent="@style/Theme.AppCompat">
...
</style>

style與theme的區(qū)別

Theme是針對窗體級別的,改變窗體樣式;
Style是針對窗體元素級別的,改變指定控件或者Layout的樣式

簡單的說就是Theme里面有包含了好多好多Style

android:theme與app:popupTheme

  • android:theme設(shè)置是View和子View的主題(API20+)
  • app:popupTheme設(shè)置的是該view節(jié)點(diǎn)下的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設(shè)置了Toolbar的主題
app:popupTheme="@style/AppTheme.PopupOverlay"設(shè)置了Toolbar節(jié)點(diǎn)下的view的主題

默認(rèn)情況下我們使用theme為android:Theme.Holo.Light.DarkActionBar那么ActionBar文字是白的,ActionBar Overflow彈出的是黑底白字,如果需求是白底黑字那么只要設(shè)置toolbar的app:popupTheme="ThemeOverlay.AppCompat.Light"


常用的自帶Theme

android:theme=”@android:style/Theme.Dialog” : Activity顯示為對話框模式
android:theme=”@android:style/Theme.NoTitleBar” : 不顯示應(yīng)用程序標(biāo)題欄 
android:theme=”@android:style/Theme.NoTitleBar.Fullscreen” : 不顯示應(yīng)用程序標(biāo)題欄,并全屏 
android:theme=”Theme.Light “: 背景為白色 
android:theme=”Theme.Light.NoTitleBar” : 白色背景并無標(biāo)題欄 
android:theme=”Theme.Light.NoTitleBar.Fullscreen” : 白色背景,無標(biāo)題欄,全屏 
android:theme=”Theme.Black” : 背景黑色 
android:theme=”Theme.Black.NoTitleBar” : 黑色背景并無標(biāo)題欄 
android:theme=”Theme.Black.NoTitleBar.Fullscreen” : 黑色背景,無標(biāo)題欄,全屏 
android:theme=”Theme.Wallpaper” : 用系統(tǒng)桌面為應(yīng)用程序背景 
android:theme=”Theme.Wallpaper.NoTitleBar” : 用系統(tǒng)桌面為應(yīng)用程序背景,且無標(biāo)題欄 
android:theme=”Theme.Wallpaper.NoTitleBar.Fullscreen” : 用系統(tǒng)桌面為應(yīng)用程序背景,無標(biāo)題欄,全屏 
android:theme=”Theme.Translucent : 透明背景 
android:theme=”Theme.Translucent.NoTitleBar” : 透明背景并無標(biāo)題 
android:theme=”Theme.Translucent.NoTitleBar.Fullscreen” : 透明背景并無標(biāo)題,全屏 
android:theme=”Theme.Panel “: 面板風(fēng)格顯示 
android:theme=”Theme.Light.Panel” : 平板風(fēng)格顯示

常用的Theme的屬性

名稱 作用
android:windowIsTranslucent 設(shè)置透明屬性(防止啟動時候的閃屏)
android:windowBackground 設(shè)置背景圖片
android:windowAnimationStyle Activity進(jìn)入退出動畫
android:windowNoTitle 不顯示標(biāo)題欄
android:textColor 默認(rèn)字體顏色
android:windowFullscreen 是否全屏
android:windowIsFloating 是否浮現(xiàn)在activity之上
android:backgroundDimEnabled 背景是否模糊顯示

常用的style元素屬性

非官方

附上一張官方的

官方

裝載自關(guān)于Android的style和theme

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容