100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > Android style(样式) 和 theme(主题) 属性

Android style(样式) 和 theme(主题) 属性

时间:2019-07-20 09:15:14

相关推荐

Android style(样式) 和 theme(主题) 属性

1. 定义

样式和主题资源都可用于对Android UI组件进行“美化”,只要充分利用好这两个属性资源,我们可以开发出各种风格的应用界面。

style 样式:一个样式相当于多个格式的集合,其他UI组件通过style属性来指定样式,样式资源文件时放在/res/values/styles.xml目录下,其根元素为<resources...../> , 该元素内可包含多个<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. 区别

theme主题一般是作用于整个 Application 或 某个 Activity

style 样式一般作用于某个控件,比如Button Layout等

3. 使用注意点

主题在新建项目的时候在AndroidManifest.xml 中一般默认配置了

<applicationandroid: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: 28833E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.themeactivitytest/com.example.themeactivitytest.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

如果你不想在AndoridManifest.xml文件中配置主题,也可以在onCreate()方法中通过代码设定

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//步骤1. 设置主题setTheme(R.style.Theme_NoTitleBar_Fullscreen);//步骤2. 加载布局setContentView(R.layout.activity_main);}如果这两个步骤顺序写反了,也会报: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" 用系统桌面为应用程序背景android:theme="@android:style/Theme.Wallpaper.NoTitleBar" 用系统桌面为应用程序背景,且无标题栏android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen" 用系统桌面为应用程序背景,无标题栏,全屏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传递纯数据,不需要界面显示的场景

需要注意事项就是需要在onResume 生命周期方法之前调用finish() 方法结束掉此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风格类似设置模块主界面比较类似,根据实际需求配置主题

6、更新

#.01.06

//继承系统内建的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 本身產品的代表色,通常也是品牌的主要視覺色

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 //正常使用

@*代表引用系统的非public资源。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。