如何使用AppCompat设置按钮的禁用颜色? [英] How do I set the disabled color of a button with AppCompat?

查看:147
本文介绍了如何使用AppCompat设置按钮的禁用颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这种样式来更改Button的背景颜色:

I use this style to change the background color of my Button:

<style name="AccentButton" parent="Widget.AppCompat.Button.Colored">
    <item name="colorButtonNormal">@color/colorAccent</item>
    <item name="android:textColor">@color/white</item>
</style>

在布局中:

    <Button
        android:id="@+id/login_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/fragment_login_login_button"
        app:theme="@style/AccentButton"/>

有效.但是,当我在此Button上调用setEnabled(false)时,它保持相同的颜色.我该如何处理这种情况?

It works. But when I call setEnabled(false) on this Button, it keeps the same color. How can I manage this case?

推荐答案

您没有正确使用Widget.AppCompat.Button.Colored样式.您正在使用父样式(Widget.AppCompat.Button.Colored),但是将其用作主题.这实际上意味着Widget.AppCompat.Button.Colored部分将被完全忽略,而您只是在更改按钮的默认颜色(可以,但是不能处理禁用的情况).

You aren't using the Widget.AppCompat.Button.Colored style correctly. You're using a parent style (Widget.AppCompat.Button.Colored), but applying it as a theme. This effectively means that the Widget.AppCompat.Button.Colored part is being ignored entirely and you are instead just changing the default color of the button (which works, but doesn't handle the disabled case).

相反,您应该使用ThemeOverlay并分别应用Colored样式:

Instead, you should use a ThemeOverlay and apply the Colored style separately:

<style name="AccentButton" parent="ThemeOverlay.AppCompat.Dark">
   <!-- customize colorButtonNormal for the disable color -->
   <!-- customize colorAccent for the enabled color -->
</style>

<Button
    android:id="@+id/login_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/fragment_login_login_button"
    android:theme="@style/AccentButton"
    style="@style/Widget.AppCompat.Button.Colored"/>

有关使用Widget.AppCompat.Button.Colored样式的答案中所述,禁用的颜色由colorButtonNormal和启用的颜色由colorAccent控制.通过使用ThemeOverlay.AppCompat.DarktextColor会自动变为暗色,这意味着您可能根本不需要自定义的ThemeOverlay.

As mentioned in this answer on using the Widget.AppCompat.Button.Colored style, the disabled color is controlled by colorButtonNormal and the enabled color is controlled by colorAccent. By using the ThemeOverlay.AppCompat.Dark, the textColor is automatically changed to dark, meaning you may not need the custom ThemeOverlay at all.

这篇关于如何使用AppCompat设置按钮的禁用颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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