材质设计:按钮不会采取主题颜色?

时间:2014-11-10 03:00:30

标签: android material-design

扩展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这样的设计指南中,似乎butttons使用了强调色。

我是否必须手动设置它们的样式?

1 个答案:

答案 0 :(得分:3)

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

<?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>

假设你有一些颜色,你喜欢默认,按下和聚焦状态。就个人而言,我通过选择中途截取了一个波纹的截图,并将主要/聚焦状态拉出来。

相关问题