Android:MaterialButton在样式中覆盖textColor [英] Android: MaterialButton override textColor in Style

查看:323
本文介绍了Android:MaterialButton在样式中覆盖textColor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想定义一个替代的按钮样式,将我的secondaryColor用作背景,并将?colorOnSecondary分别用作文本.

I want to define an alternative Button Style that uses my secondaryColor as background and the ?colorOnSecondary respectively for the text.

但是我在努力以一种样式定义textColor. MaterialButton对使用?colorOnPrimary的textColor使用(专用)选择器可绘制. 因此,这很麻烦.

But I'm struggling to get the textColor defined in a style. The MaterialButton is using a (private) selector-drawable for the textColor which uses ?colorOnPrimary. So this is cumbersome to override.

是否有另一种方法来设置没有selectorSelectable的颜色?

Is there another way to set the color without the selectorDrawable?

推荐答案

如果只想覆盖按钮的颜色,则可以使用 materialThemeOverlay 属性(它需要版本1.1) .0).
像这样:

If you want to override the colors only for the buttons, you can use the materialThemeOverlay attribute(it requires the version 1.1.0).
Something like:

  <style name="MyButtonTheme" parent="Widget.MaterialComponents.Button">
    <item name="materialThemeOverlay">@style/ButtonStyleTextColor</item>
  </style>

  <style name="ButtonStyleTextColor">
    <item name="colorOnPrimary">@color/...</item>
    <item name="colorOnSecondary">@color/...</item>
    <item name="colorOnSurface">....</item>
    .....
  </style>

否则,您可以定义自定义样式:

<style name="CustomButtonStyle" parent="Widget.MaterialComponents.Button">
  <item name="backgroundTint">@color/my_bg_color_selector</item>
  <item name="android:textColor">@color/my_text_color_selector</item>
</style> 

my_bg_color_selector.xml可能类似于:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="@color/..." android:state_enabled="true"/>
  <item android:alpha="0.12" android:color="@color/..."/>
</selector>

my_text_color_selector.xml可以是:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="@color/..." android:state_enabled="true"/>
  <item android:alpha="0.38" android:color="@color/..."/>
</selector>

这篇关于Android:MaterialButton在样式中覆盖textColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆