【问题标题】:material design: buttons won't take theme color?材料设计:按钮不会采用主题色?
【发布时间】:2015-01-06 07:11:44
【问题描述】:

扩展 material.light 主题后,设置颜色如下:

<resources>
    <!-- inherit from the material theme -->
    <style name="MiTemaMaterial" parent="android:Theme.Material.Light">
        <!-- Main theme colors -->
        <!--   your app branding color for the app bar -->
        <item name="android:colorPrimary">#4DB6AC</item>
        <!--   darker variant for the status bar and contextual app bars -->
        <item name="android:colorPrimaryDark">#009688</item>
        <!--   theme UI controls like checkboxes and text fields -->
        <item name="android:colorAccent">#FFAB40</item>
    </style> </resources>

我得到了一个类似材质设计的导航栏,但按钮等视图不采用配色方案并显示为默认值。我想知道这是否是标准行为,因为在http://www.google.com/design/spec/components/buttons.html 这样的设计指南中,按钮似乎使用强调色。

我必须手动设置样式吗?

【问题讨论】:

    标签: android material-design


    【解决方案1】:

    是的,您需要自己为按钮设置主题。要提供一个采用原色的按钮,请使用 v21 目录中的可绘制对象作为背景,例如:

    <?xml version="1.0" encoding="utf-8"?>
    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
            android:color="?attr/colorControlHighlight">
        <item android:drawable="?attr/colorPrimary"/>
    </ripple>
    

    这将确保您的背景颜色为?attr/colorPrimary,并使用默认的?attr/colorControlHighlight 使用默认的波纹动画(如果您愿意,也可以在主题中设置)。

    注意:您必须为低于 v21 创建一个自定义选择器:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@color/primaryPressed" android:state_pressed="true"/>
        <item android:drawable="@color/primaryFocused" android:state_focused="true"/>
        <item android:drawable="@color/primary"/>
    </selector>
    

    假设您对默认、按下和聚焦状态有一些您想要的颜色。就个人而言,我在被选中的过程中截取了一个涟漪的屏幕截图,并将主要/聚焦状态从中拉出来。

    【讨论】:

    • 我明白了,谢谢!而且我想我还必须手动为所有其他视图设置主题,例如单选按钮等,对吧?
    • 大多数组件会自动使用部分主题颜色。尝试all of the theme colors 可能会有所帮助
    猜你喜欢
    • 2019-02-28
    • 1970-01-01
    • 2019-08-12
    • 2019-10-11
    • 2019-11-01
    • 1970-01-01
    • 2016-06-28
    • 2015-01-04
    • 1970-01-01
    相关资源
    最近更新 更多