这章来说,主要是来使用样式和主题,定制出漂亮的用户的界面
(1)颜色资源
我们需要在res/values中编辑color.xml,使用颜色资源就可以方便在一处定义各种颜色值,然后在整个应用中去使用
(2)样式
在res/values/styles.xml文件(在这里面可以设置自己想要的属性然后就可以去使用了)中,添加BeatBoxButton新样式】
<style name = "BeatBoxButton" >
<item name = "android:background">@color/dark_blue</item>
</style>
新建了一个样式并设置了属性,然后我们要更新属性时只要更新公共属性就会了
<Button
style = "@stylr/BeatBoxButton"//这里就是运用了
/>
(3)样式继承,样式支持继承,一个样式能继承并覆盖其他样式的属性
<style name = "BeatBoxButton" >
<item name = "android:background">@color/dark_blue</item>
</style>
<style name = "BeatBoxButton.Strong">//这个命名表明这个新样式继承了BeatBoxButton样式的属性,其实也可以直接在上面那个样式中添加下面的属性的
<item name = "android:textStyle">bold</item>
除了命名表示样式表示继承关系,也可以采用指定的父样式的方式:
<style name = "StrongBeatBoxButton" parent = "@style/BeatBoxButton">
<item name = "android:textStyle">bold</item>
</style>
(4)主题
对于样式来说,它可以为所有组件定义一套样式属性共用的,定义公共属性容易,但是实际应用确实很麻烦,需要一个一个去添加他们需要用到的样式,如果一个应用太过复杂,有着许多布局和按钮,这样会累死人,所以此时就应该选择使用主题来应用整个应用。
修改默认的主题,我们创建项目的时候其实已经是创建了它默认的主题,找到Androidmanifest文件,可以看见application标签下面的thema属性
android:theme= "@style/AppTheme"
我们可以看见主题其实也是一种样式,但是主题指定的属性有别于样式
我们现在编写的项目大多会自带AppCompat主题
AppCompat库自带三大主题
Theme.AppCompat 深色主题
Theme.AppCompat.Light 浅色主题
Thema.AppCompat.Light.DarkActionBar 带有深色工具栏的浅色主题
我们通过改变parent = " ",双引号里面的内容就可以来创建不同的主题
(5)添加主题颜色
在style.xml文件中,我们根据AppTheme的模板上来自定义主题的属性
<style name = "AppTheme" parent = "Theme.AppCompat">
<item name = "colorPrimary" >@color/red</item>//工具栏会以这个来用于自己的背景色
<item name = "colorPrimaryDark">@color/dark</item>//这个是用于屏幕顶部的状态栏,也就是平时我们看见电路那个行
<item name = "colorAccrnt">@color/gray</item>//这个的颜色是为了与colorPrimary形成反差结果,主要是用于EditText这种控件
</stylle>
(6)覆盖主题的属性
如果我们想要实现修改主题以来更换BeatBox应用的背景色,我们可以在res/layout/fragment_beat_box.xm;文件,手工设置RecyclerView中的android:bacground属性,但主题一i就设置了背景色,就不应该在此基础上去设置其他颜色了,我们应该去覆盖主题的颜色
所以接下来我们就需要来进行主题探索了,
我们打开style.xml文件,按住ctrl然后点击Theme.AppCompat,现在就来看看继承有多么深了,进去然后进入另一个文件,看看有没有那个属性,没有的话就继续按照上面的步骤,但是当有多个版本让我们进行选择时,我们应该选择values/values.xml这个版本,找到最后我们可能会看见下面的代码
<style name = "Platform.AppCompat" parent = "android:Theme"
这边我们可以看出parent后面的代码与之前看见的不一样,这是因为有些主题包含在Android的操作系统中,此时引用就必须加上归属地的命名空间,到这边我们应该可以看到我们需要覆盖的属性了,这里也就是 android:windowBackground 这个属性现在返回到style.xml文件中,我们就应该按下面的代码那样去覆盖
<style name = "AppTheme" parent = "Theme.AppCompat">
<item name = "colorPrimary" >@color/red</item>//工具栏会以这个来用于自己的背景色
<item name = "colorPrimaryDark">@color/dark</item>//这个是用于屏幕顶部的状态栏,也就是平时我们看见电路那个行
<item name = "colorAccrnt">@color/gray</item>//这个的颜色是为了与colorPrimary形成反差结果,主要是用于EditText这种控件
<item name = "androdi:windowBackground">@color/sooting_blue</item>//覆盖的属性
</stylle>
现在我们就可以发现,没有按钮覆盖的地方就是浅蓝色的了
总结;以后如果想要修改应用主题,我们应该按照上面的步骤去找到某个属性,然后回到style.xml文件里面去覆盖这个属性
(7)修改按钮的属性
在前面我们通过在res/layout/list_item_sound.xml文件中手工设置样式属性,我们定制了自己的按钮属性,但是如果在复杂的应用中我们有许多按钮,我们如果通过逐个设置,那么就太麻烦了,这时候我们就应该靠主题了
1.我们需要先逐级查找主题来查找除buttonStyle属性,当查到那个拥有按钮的属性的主题后,就在style.xml文件中去新新建一个style然后去继承那个我们找到的主题,然后选择性地修改一些属性
<style name = "BeatBoxButton" parent = "Widget.AppCompat.Button"> parent后面的主题就是我们找到的主题
<item name = "android:background">@color/dark_blue</item>
</style>
2然后我们就在主题里面去覆盖buttonStyle属性,然后让它指向我们上面的BeatBoxButton样式
<style name = "AppTheme" parent = "Theme.AppCompat">
<item name = "colorPrimary" >@color/red</item>//工具栏会以这个来用于自己的背景色
<item name = "colorPrimaryDark">@color/dark</item>//这个是用于屏幕顶部的状态栏,也就是平时我们看见电路那个行
<item name = "colorAccrnt">@color/gray</item>//这个的颜色是为了与colorPrimary形成反差结果,主要是用于EditText这种控件
<item name = "androdi:windowBackground">@color/sooting_blue</item>//覆盖的属性
<item name = "buttonStyle" >@style/BeatBoxButton</item>
</stylle>
现在buttonStyle已经覆盖了,我们已经使用了自定义的BeatBoxButton,这样没有改变布局我们就改变了普通按钮的样子
(8)引用主题的属性
在主题中要引用资源时,我们需要使用?符号,如下面的代码
<Button xmlns:android= "http://schemas.aandroid.com/apk/res/android"
android:background = "?attr/corlorAccent"//意思时指向colorAccent指向的资源
/>
在代码中来实现
Resources.Thema theme = getActivity().getTheme();
int[] attrsToFetch = {R.attr.colorAccent};
TypedArray a = theme.obtainStyledAttributes(R.style.AppTheme,arrrsToFetch);
int accentColor = a.getInt(0,0);
a.recycle();