如何使用AppCompat设置禁用的按钮颜色?

Ale*_*ndr 34 android android-appcompat android-button

我使用这种风格来改变我的背景颜色Button:

<style name="AccentButton" parent="Widget.AppCompat.Button.Colored">
    <item name="colorButtonNormal">@color/colorAccent</item>
    <item name="android:textColor">@color/white</item>
</style>
Run Code Online (Sandbox Code Playgroud)

在布局中:

    <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"/>
Run Code Online (Sandbox Code Playgroud)

有用.但是当我打电话setEnabled(false)给它时Button,它保持相同的颜色.我该如何处理这个案子?

ian*_*ake 78

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

相反,您应该使用a ThemeOverlay并Colored单独应用该样式:

<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"/>
Run Code Online (Sandbox Code Playgroud)

正如在使用该Widget.AppCompat.Button.Colored样式的答案中所提到的,禁用的颜色由受控制,colorButtonNormal并且启用的颜色由其控制colorAccent.通过使用ThemeOverlay.AppCompat.Dark,textColor自动更改为暗,这意味着您可能根本不需要自定义ThemeOverlay.

  • 当我们应用彩色风格时,有没有人注意到"涟漪效应"缺失? (2认同)

Mea*_*man 11

将接受的解决方案与自定义小部件相结合,我们可以通过设置alpha来显示禁用的按钮.这适用于任何按钮和文本颜色组合:

public class ButtonWidget extends AppCompatButton {

    public ButtonWidget(Context context) {
        super(context);
    }

    public ButtonWidget(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ButtonWidget(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setEnabled(boolean enabled) {
        setAlpha(enabled ? 1 : 0.5f);
        super.setEnabled(enabled);
    }

}
Run Code Online (Sandbox Code Playgroud)


小智 8

您应该使用带有选择器的背景,而不是使用按钮的颜色.这是演示代码

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true">
        <shape android:shape="rectangle">
            <solid android:color="@color/yourEnabledColor" />
        </shape>
    </item>
    <item android:state_enabled="false">
        <shape android:shape="rectangle">
            <solid android:color="@color/yourDisabledColor" />
        </shape>
    </item>
</selector>
Run Code Online (Sandbox Code Playgroud)


Mil*_*nák 7

目前,我将以下设置用于Android API 15+。

/res/color/btn_text_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="#42000000" android:state_enabled="false" />
  <item android:color="#ffffff" />
</selector>
Run Code Online (Sandbox Code Playgroud)

/res/values/styles.xml

<style name="ColoredButton" parent="Widget.AppCompat.Button.Colored">
    <item name="android:textColor">@color/btn_text_color</item>
</style>
Run Code Online (Sandbox Code Playgroud)

和

<Button
    android:id="@+id/button"
    style="@style/ColoredButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button" />
Run Code Online (Sandbox Code Playgroud)


Ami*_*ela 5

当您以编程方式更改时,您需要采取这种方式:

button = new Button(new ContextThemeWrapper(ActiVityName.this, R.style.AccentButton));
Run Code Online (Sandbox Code Playgroud)

要么

if (button.isEnabled())
    button.getBackground().setColorFilter(Color.Black, PorterDuff.Mode.MULTIPLY);
else
    button.getBackground().setColorFilter(null);
Run Code Online (Sandbox Code Playgroud)