1. 定義
????????樣式和主題資源都可用于對Android UI組件進行“美化”,只要充分利用好這兩個屬性資源,我們可以開發(fā)出各種風格的應用界面。
????????style 樣式: 一個樣式相當于多個格式的集合,其他UI組件通過style屬性來指定樣式,樣式資源文件時放在/res/values/styles.xml目錄下,其根元素為<resources...../> , 該元素內(nèi)可包含多個<style..../>子元素,每個 <style.../>子元素定義一個樣式。
<style..../>元素指定如下兩個屬性
????????name : 指定樣式的名稱
????????parent : 指定繼承父類的樣式:
<resources>
<style name="system.theme.test" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
????????theme 主題:與樣式資源相似,主題資源的xml文件也是放在/res/values/themes.xml,? XML文件同樣根元素為<resources...../>,同樣使用<style..../>元素來定義主題
<resources>
<style name="Theme.DeskClock.Settings">
<!-- Attributes from androidx.appcompat.appcompat -->
<item name="actionBarStyle">@style/Widget.ActionBar</item>
<item name="colorControlActivated">?attr/colorAccent</item>
<item name="windowActionBar">true</item>
<item name="windowNoTitle">false</item>
</resources>
2. 區(qū)別
? ? ? ? theme主題一般是作用于整個 Application 或 某個 Activity
? ? ? ? style 樣式一般作用于某個控件,比如Button Layout等
3. 使用注意點
? ? ? ? 主題在新建項目的時候在AndroidManifest.xml 中一般默認配置了
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
// 默認主題
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
? ? 假如我們把?android:theme="@style/AppTheme" 去掉,就會報如下錯誤:
? ? 你必須要給這個app設定一個主題
E AndroidRuntime: Process: com.example.themeactivitytest, PID: 28833
E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.themeactivitytest/com.example.themeactivitytest.MainActivit
y}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
? ? 如果你不想在AndoridManifest.xml文件中配置主題,也可以在onCreate()方法中通過代碼設定
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//步驟1. 設置主題
setTheme(R.style.Theme_NoTitleBar_Fullscreen);
//步驟2. 加載布局
setContentView(R.layout.activity_main);
}
如果這兩個步驟順序?qū)懛戳?,也會報:AndroidRuntime: Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 錯誤
4. 常用的主題
常見的主題風格有:
android:theme="@android:style/Theme.Dialog" 灰色背景 將一個Activity顯示為對話框模式
android:theme="@android:style/Theme.NoTitleBar" 灰色背景 不顯示應用程序標題欄
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 灰色背景,不顯示應用程序標題欄,并全屏
android:theme="@android:style/Theme.Light" 背景為白色
android:theme="@android:style/Theme.Light.NoTitleBar" 白色背景并無標題欄
android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" 白色背景,無標題欄,全屏
android:theme="@android:style/Theme.Black" 背景黑色
android:theme="@android:style/Theme.Black.NoTitleBar" 黑色背景并無標題欄
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" 黑色背景,無標題欄,全屏
android:theme="@android:style/Theme.Wallpaper" 用系統(tǒng)桌面為應用程序背景
android:theme="@android:style/Theme.Wallpaper.NoTitleBar" 用系統(tǒng)桌面為應用程序背景,且無標題欄
android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen" 用系統(tǒng)桌面為應用程序背景,無標題欄,全屏
android:theme="@android:style/Theme.Translucent" 半透明
android:theme="@android:style/Theme.Translucent.NoTitleBar" 半透明、無標題欄
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" 半透明、無標題欄、全屏
android:theme="@android:style/Theme.Panel" 面板風格顯示
如果配置為? ?android:theme="@android:style/Theme.NoDisplay"? 此界面不顯示,一般用于Activity傳遞純數(shù)據(jù),不需要界面顯示的場景
需要注意事項就是需要在onResume 生命周期方法之前調(diào)用finish() 方法結(jié)束掉此Activity,否則報錯:E AndroidRuntime: Caused by: java.lang.IllegalStateException: Activity? did not call
finish() prior to onResume() completing
5. 更多主題
? ? ? ? 可以查看源碼? ? ? ? ?樣式 和? 主題路徑:
? ? ? framework/base/core/res/res/values/themes.xml
? ? ? framework/base/core/res/res/values/themes_material.xml? ? ? ?M- design風格的
? ? ? framework/base/core/res/res/values/styles.xml ?
? ? ? framework/base/core/res/res/values/styles_material.xml? ? ? ??M- design風格的
android:theme="@android:style/Theme.Material.NoActionBar.Fullscreen" M風格 無標題,全屏主題
android:theme="@android:style/Theme.Material.Light.Dialog" Material風格 白色 Dialog主題
android:theme="@android:style/Theme.Material.Wallpaper" Material風格 背景為壁紙的主題
android:theme="@android:style/Theme.Material.Settings" Material風格類似設置模塊主界面
比較類似,根據(jù)實際需求配置主題
6、更新
#2023.01.06
//繼承系統(tǒng)內(nèi)建的style(繼承Framework中theme的屬性是需要“android:”開頭)
<style name="GreenText" parent="@android:style/TextAppearance">
<item name="android:textColor">#00FF00</item>
</style>
//繼承自AppCompat下的style(繼承Support Library中theme的屬性是不需要“android:”開頭的)
<style name="Theme.ThemeActivity" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:textColor">#00FF00</item>
</style>
Material Color system
如果使用的是Material Theme,它提供了很多的 color attribute 使用:
colorPrimary : 顧名思義,就是主要的顏色,這個通常指得是 App 本身產(chǎn)品的代表色,通常也是品牌的主要視覺色
colorPrimaryVariant:主要顏色的變體,通常會從 colorPrimary 往較淡或較濃的色澤
colorOnPrimary:字面意思就是主要顏色上頭的顏色,這個顏色通常使用在背景色是主要顏色的元件上頭(例如字樣 Label 、icon 等)
colorSecondary:app 次要的品牌顏色,這些用於裝飾某些特定需要的 widget
colorSecondaryVariant:次要顏色的變體,也就是次要顏色偏暗或偏亮的樣式
colorOnSecondary:用於顯示於次要顏色上元件的顏色
colorError:顯示錯誤的顏色 (最常見的就是紅色)
colorOnError:在錯誤顏色上頭元件的顏色
colorSurface:表層顏色(就是 Sheet 的顏色)
colorOnSurface:在表層顏色上的的元件顏色
android:colorBackground:最底的背景色
colorOnBackground:用於對底背景色上頭的元件用的顏色
利用這些屬性,搭配上面的那些技巧,可以組合出很棒的效果。
?
引用私有style報錯:
AAPT: error: resource android:style/Theme.Leanback.Dialog is private.
error: failed linking references.
解決方法:
之前寫法:
@android:style/Theme.Leanback.Dialog? ? ?//報錯
改為引用私有資源:? ?加上*號
@*android:style/Theme.Leanback.Dialog? ? //正常使用文章來源:http://www.zghlxwxcb.cn/news/detail-411206.html
@*代表引用系統(tǒng)的非public資源。
?文章來源地址http://www.zghlxwxcb.cn/news/detail-411206.html
到了這里,關于Android style(樣式) 和 theme(主題) 屬性的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!