zoukankan      html  css  js  c++  java
  • 这些Android系统样式中的颜色属性你知道吗?

    Android 系统样式中的颜色属性

    推荐阅读看完后彻底搞清楚Android中的 Attr 、 Style 、Theme

    几个常用的颜色属性

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

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

    在开发者官网 R.attr 中给我们列出了所有的系统属性,我们可以在这里面找到对应的颜色属性所代表的意思。

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

    <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 系列就可以了。

    关于 AppCompat 相关主题提供的系统属性,可以参考源码:https://android.googlesource.com/platform/frameworks/support/+/400d2df7dfb0f72117b84854035829b6eaaf3150/v7/appcompat/res/values-v21/themes_base.xml

    同时由于部分属性的版本兼容问题,为了避免添加多个版本的 styles 文件,可以省略 android: 命名空间

    colorPrimary

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

    <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 一样里面有那么多的内容。

    参考:https://juejin.im/post/58f8b651b123db006238dd8d

  • 相关阅读:
    Balanced Binary Tree
    Convert Sorted List to Binary Search Tree
    Convert Sorted Array to Binary Search Tree
    Binary Tree Zigzag Level Order Traversal
    Validate Binary Search Tree
    Binary Tree Level Order Traversal II
    Binary Tree Level Order Traversal
    Maximum Depth of Binary Tree
    如何把U盘的两个盘或者多个盘合成一个
    bugku 想蹭网先解开密码
  • 原文地址:https://www.cnblogs.com/sydmobile/p/11807087.html
Copyright © 2011-2022 走看看