【问题标题】:Material design : coloring two buttons with two colors in Android材料设计:在Android中用两种颜色为两个按钮着色
【发布时间】:2015-03-11 19:14:27
【问题描述】:

我一直在寻找一种在 android 5.0 中处理按钮背景颜色的正确方法,我能找到的唯一解决方案是在 values-21/styles.xml 中为按钮定义样式:

<item name="android:colorButtonNormal">#2196f3</item>

这是为按钮着色的唯一方法,同时保留其设计和涟漪效果吗? 如果是的话,这意味着我必须为每个具有不同颜色的按钮定义一个自定义主题,真的???

注意:我的问题与向后兼容性和 AppCompat 无关,这已经讨论了很多。

【问题讨论】:

  • 这没有回答问题,因为它意味着首先设置背景颜色,以便让 backgroundTint 工作。正如我的问题中提到的,我正在寻找一种方法来保留按钮的材质方面。所以我剩下的唯一解决方案是使用不同的样式并在按钮上设置主题属性?
  • 基本上是的。当前版本的 Lollipop 存在一个错误,即 GradientDrawable 不考虑色调,因此唯一的选择是创建自己的背景或在 Button 上使用 android:theme 来覆盖 colorButtonNormal 属性的值。
  • 谢谢阿兰夫。我决定遵循多风格的方式,效果很好,让我能够保持物质方面。
  • 人们可以评论否决票吗?

标签: android button material-design tint


【解决方案1】:

您可以使用 tint 选项以这种方式更改颜色,而不是为每个按钮制作单独的主题,您只需将每个按钮着色为不同的颜色。见这里:Material Design Drawables

【讨论】:

  • 如果答案如此琐碎,我就不会问这个问题。除非您设置背景颜色,否则设置背景色调不会改变任何内容,因此会丢失材质格式。
【解决方案2】:

是的,您必须使用colorButtonNormal 为每种颜色定义一个单独的主题。您可以将主题设置为您的Button,如下所示:

<style name="ColorButonButton" parent="Theme.AppCompat.Light">  
    <item name="colorButtonNormal">@color/newColor</item>
</style>

<Button  
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Text"
    android:theme="@style/ColorButonButton"/>

【讨论】:

    猜你喜欢
    • 2015-08-15
    • 1970-01-01
    • 2018-12-22
    • 2014-10-12
    • 2015-08-13
    • 2019-02-28
    • 1970-01-01
    • 2015-03-14
    • 2012-02-05
    相关资源
    最近更新 更多