样式和主题(Style and Theme)详解

导读:

本篇文章主要根据官方文档修改,介绍了样式和主题的使用以及要注意的问题,同时也提供了Android Studio 快速抽取Style 和 编辑 Theme 的方法.

Tips

使用Android Studio 的同学,可以直接在布局文件对应控件:
右键 -> Refactor -> Extract -> Style 抽取样式
右键 -> Refactor -> Extract -> Layout 抽取布局 include标签

样式简介:

样式是指为 View 或窗口指定外观和格式的属性集合.样式可以指定高度、填充、字体颜色、字号、背景色等许多属性. 样式是在与指定布局的 XML 不同的 XML 资源中进行定义.

因此我们可以使用样式将xml文件中的控件属性抽取简化

设置样式的方式有两种:

  • 如果是对单个视图应用样式,在布局 XML 中的 View 元素添加 style 属性.

  • 如果是对整个Activity或应用来应用样式,在 Android 清单中的或节点添加android:theme属性.

系统自带的Theme and Style的标准属性文档

  • R.styleable.Theme该链接提供了系统自带的,可在主题(Theme)中使用的标准属性的列表

  • R.style该链接可查到系统自带的,在样式(Style)中使用的标准属性的列表


单个视图View的使用

一、在res/values/目录下自定义以节点的.xml文件(或直接在styles.xml也行)定义我们想要实现的样式

简化TextView为例

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CodeFont" >
        <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="Hello World!"
        />

继承

  • 样式具有继承关系,可以通过

   //.xml文件,@是说明系统已经定义过的,@android:style/  是必须带上的
    <style name="RedText" parent="@android:style/TextAppearance">
        <item name="android:textColor">#FF0000</item>
    </style>

  //控件
   <TextView
        style="@style/RedText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        />

如果想要继承我们自定义的


<style name="CodeFont.Blue">
        <item name="android:textColor">#0000FF</item>
    </style>

    <style name="CodeFont.Blue.Big">
        <item name="android:textColor">#0000FF</item>
        <item name="android:textSize">30sp</item>
    </style>

注意 : 句点链接名称的继承方式只适用于我们自定义的资源样式.无法继承Android内建样式,引用内建样式必须使用parent属性.

Actvity或Application使用(Theme主题的使用)

声明主题的例子:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <style name="CustomTheme">
 <item name="android:windowNoTitle">true</item>
 <item name="windowFrame">@drawable/screen_frame</item>
 <item name="windowBackground">@drawable/screen_background_white</item>
 <item name="panelForegroundColor">#FF000000</item>
 <item name="panelBackgroundColor">#FFFFFFFF</item>
 <item name="panelTextColor">?panelForegroundColor</item>
 <item name="panelTextSize">14</item>
 <item name="menuItemTextColor">?panelTextColor</item>
 <item name="menuItemTextSize">?panelTextSize</item>
 </style>
</resources>

注意 : 我们用了@符号和?符号来应用资源.@符号表明了我们应用的资源是前边定义过的(或者在前一个项目中或者在Android 框架中).问号?表明了我们引用的资源的值在当前的主题当中定义过.通过引用在里边定义的名字可以做到(panelTextColor 用的颜色和panelForegroundColor中定义的一样)。这种技巧只能用在XML资源当中.


  • 要为所有Activity设置主题,在AndroidManifest.xml清单文件中的节点,加入带样式名称的android:theme属性
<application android:theme="@style/CustomTheme">
  • 要为应用中某一个Activity应用主题,在AndroidManifest.xml清单文件中的节点,加入带样式名称的android:theme属性
<activity android:theme="@android:style/Theme.Dialog"> //系统的

<activity android:theme="@style/CustomTheme"> //自定义的
  • 如果想用某个系统自带的主题,但想做些调整,可以使用样式的继承属性,然后修改想改的属性

<color name="custom_theme_color">#b0b0ff</color>
<style name="CustomTheme" parent="android:Theme.Light">
    <item name="android:windowBackground">@color/custom_theme_color</item>
    <item name="android:colorBackground">@color/custom_theme_color</item>
</style>

==注意==

  1. 句点链接名称的继承方式只适用于我们自定义的资源样式.无法继承Android内建样式,引用内建样式必须使用parent属性.
  2. style属性不使用android:命名空间前缀 如:style=”@style/RedText”,style=..前
  3. @符号表明了我们应用的资源是前边定义过的(或者在前一个项目中或者在Android 框架中)。问号?表明了我们引用的资源的值在当前的主题当中定义过。通过引用在里边定义的名字可以做到(panelTextColor 用的颜色和panelForegroundColor中定义的一样)。这中技巧只能用在XML资源当中。
  4. style 和 Theme都具有继承属性,只是最终调用的位置不同而已

根据平台版本选择主题

如,在res/values/目录下的一个XML文件(通常是res/values/styles.xml):


<style name="LightThemeSelector" parent="android:Theme.Light">
    ...
</style>

那么要让该主题在运行在Android3.0 (API 11)或以上的版本,我们可以在res/values-v11目录下的的XML文件中声明一个


<style name="LightThemeSelector" parent="android:Theme.Holo.Light">
    ...
</style>

==有可能出现的Bug==:

  1. 如果写完代码,代码上什么错误都没报,一运行程序就闪退,看到 android.support.v7.view.WindowCallbackWrapper.onSearchRequested 这句,代表v7包下的主题(Theme)用错了,把parent的android删掉即可

  2. 注意我们继承的系统主题的对应属性的命名方式前是否有android字样,不对应的话会报This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR类似的错误


Android Studio Style快速抽取:

右键->Refactor->Extract->Style->ok就会抽取到res/values/styles.xml了

这里写图片描述

Android Studio Theme Editor(主题编辑器)

  1. 工具栏Tools->Android->Theme Editor
  2. 打开Styles.xml文件,右上角有Open editor按钮,点击打开

这里写图片描述

左边展示主题修改后的预览结果,右边是主题编辑器窗口,点击小箭头,可以创建一个新的主题控件.

这里写图片描述

接下来就可以根据下表属性,乱嗨了,不过记得选择完想要的颜色,记住给它起个新名字,不然会覆盖AppTheme默认的颜色

属性 说明
colorPrimary 应用的主色调,ActionBar默认使用该颜色,ToolBar导航栏的底色
colorPrimaryDark 应用的主要暗色调,StatusBar状态栏默认使用改颜色
colorAccent 控件选中的默认颜色,如EditText 的闪动光标
android:colorControlNormal 控件未选中时的默认颜色,如复选框
android:textColorPrimary 应用的主要文字颜色,ActionBar的标题文字默认颜色
android:textColorSecondary 辅助的文字颜色,一般比textColorPrimary的颜色弱一点,用于一些弱化的表示
android:windowBackground 窗体背景颜色,必须用color.xml定义的颜色
android:navigationBarColor 底部操作栏颜色 API>21
扩展属性 扩展说明
statusBarColor 状态栏颜色,默认使用colorPrimaryDark
colorForeground 应用的前景色,ListView的分割线,switch滑动区默认使用该颜色
colorBackground 应用的背景色,popMenu的背景默认使用该颜色
colorControlHighlight 控件按压时的色调
colorControlActivated 控件选中时的颜色,默认使用colorAccent
colorButtonNormal 默认按钮的背景颜色
editTextColor 默认EditView输入框字体的颜色。
textColor Button,textView的文字颜色
textColorPrimaryDisableOnly RadioButton,checkbox等控件的文字
colorSwitchThumbNormal switch thumbs 默认状态的颜色. (switch off)

最后记得在AndroidManifest清单文件,在或添加android:theme,调用我们设置好的主题

总结

本篇文章到此结束,欢迎关注,后续有补充的会即使更新,有问题也欢迎评论,共同成长

版权声明:本文为博主原创或汇集文章,欢迎注明来源转载。http://blog.csdn.net/u012792686 https://blog.csdn.net/u012792686/article/details/72852346

猜你喜欢

转载自blog.csdn.net/daimengs/article/details/81186379