这些Android系统样式中的颜色属性你知道吗?

Android 系统样式中的颜色属性html

推荐阅读看完后完全搞清楚Android中的 Attr 、 Style 、Themeandroid

几个经常使用的颜色属性

先放上一张经典的图片,图片来自网络。git

这张图在网上非常流传,也不知道当初是哪位大神标注的,很好的说明了 Android 系统中的几个经常使用的颜色属性的做用范围。github

在开发者官网 R.attr 中给咱们列出了全部的系统属性,咱们能够在这里面找到对应的颜色属性所表明的意思。网络

一般咱们新建一个项目的时候在 res/values/styles.xml 中会有下面的定义 Application 的主题样式。app

<resource>
	    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style>
</resource>
复制代码

其实样式远远不止如此,在 Android 5.0 开始 Android 系统引入了 Material Design 风格,各个控件在这样主题下面,风格有很大的变化。为了在不一样版本的系统中统一 UI 样式,设置自定义的 Theme 继承自 Theme.AppCompat 系列就能够了。post

关于 AppCompat 相关主题提供的系统属性,能够参考源码:android.googlesource.com/platform/fr…字体

同时因为部分属性的版本兼容问题,为了不添加多个版本的 styles 文件,能够省略 android: 命名空间google

colorPrimary

App Bar 的背景颜色,也是一个 APP 的主色调。不过 ActionBar 已经不鼓励使用了,由 Toolbar 来代替,须要给 Toolbar 来设置背景颜色。spa

<android.support.v7.widget.Toolbar android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" />
复制代码

colorPrimaryDark

status bar(状态栏)的背景色,仅适用于 Android 5.0及其以上版本。也就是在这个版本你直接能够经过修改这个属性来修改状态栏的颜色。

colorAccent

许多控件在选中状态或者获取到焦点的时候会使用这个颜色,好比:

  • CheckBox:checked 状态
  • RadioButton:checked 状态
  • SwitchCompat:checked 状态
  • EditText:获取焦点的时候的下划线和光标颜色
  • TextInputLayout:悬浮 label 字体颜色
  • 等等

android:navigationBarColor

navigation bar 的背景色,仅用于 Android 5.0及其以上

colorControlNormal

某些 View 处于普通状态下的颜色。

好比:

  • 没有被选中的 CheckBox 或者没有被选中的 RadioButton

  • 失去焦点时的 EditText ,Toolbar 溢出按钮颜色

  • 等等

colorControlActivated

在某些时候 colorControlActivatedcolorAccent 的替代品。这种状况下 colorControlActivate 的颜色是会覆盖 colorAccent 的颜色的。也就是说,若是你没有设置的话默认的颜色就是 colorAccent 的颜色

好比:

  • CheckBox 和 RadioButton 的 checked 状态

colorControlHightlight

全部可点击 View 触摸状态下的 Ripple 效果。仅对 Android 5.0 及其以上有效

colorButtonNormal

Button normal 状态下的背景色。

这种设置和 Button 的 android:background 所不一样的是,在 Android5.0 或者更高的版本上使用 colorButtonNormal 的时候会依然保持阴影和 Ripple 触摸效果

android:windowBackground

窗口背景色,诸如此类还有 android:background android:colorBackground

android:textColorPrimary

APP 的主要文字颜色,好比 actionbar

文本的颜色,好比 Button 中的文本颜色,EditText 中的文本颜色,AlertDialog 中的文本颜色。可是不包括 TextView 中的文字颜色,TextView 中的文字颜色还须要 TextColor 来控制。

固然在设置了 TextColor 的话,TextColor 优先。

editTextColor:

默认 EditView 输入框字体颜色

TextColor

TextView 的文字颜色

更多查看这里

样式介绍

从 Android 5.0 开始,Android 系统引入了 Material Design风格。

md 的主题有:

  • @android:style/Theme.Material(暗主题)
  • @android:style/Theme.Material.Light(亮主题)
  • @android:style/Theme.Material.Light.DarkActionBar

固然了为了兼容性,咱们通常使用j兼容包里面的 Them.AppCompat 主题

与之对应的样式主要有 Them.AppCompatThemeOverlay.AppCompat ,固然他们两个又有各自的子类。

这两种样式也有不一样的使用方法

Theme.AppCompat 通常用于设置整个应用程序的全局主题

ThemeOverlay.AppCompat 用于覆盖特定视图的主题,覆盖相关的属性使他们成为亮或者暗 尤为是在 Toolbar 中运用。

这样说能够不太容易明白,下面经过一个例子来讲明。

好比说先个人整个 APP 的主题是这样的

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style>
复制代码

默认的文字的颜色是黑的的,那么显示效果就会是这样的

这里使用了一个 Titlbar 这样效果和总体很不搭配,咱们须要的是 Titlbar 的背景使用咱们的 colorPrimary 字体的颜色使用 浅色文本样式 的颜色,那么这样咱们就能够本身定义一个样式

<style name="sencond" parent="ThemeOverlay.AppCompat.Dark.ActionBar"> <item name="android:background">@color/colorPrimary</item> </style>
复制代码

<androidx.appcompat.widget.Toolbar android:theme="@style/sencond" app:title="@string/app_name" android:layout_width="match_parent" android:layout_height="wrap_content"/>
复制代码

而后把这个样式给 Titlbar设置上,这样效果就合适了。

固然你本身彻底能够用属性来本身完成。

这是 ThemOverlay 样式的所有样式了,每一个样式里面的内容都很简单。就是修改一些最基本的属性,不像 Theme 同样里面有那么多的内容。

参考:juejin.im/post/58f8b6…

相关文章
相关标签/搜索