使用 AppCompat 为 Android 中的按钮着色

Coloring Buttons in Android with AppCompat

有谁知道如何更改按钮颜色?

只是一个特定的按钮,而不是使用 XML 的应用程序的所有按钮。

The android:backgroundTint attribute doesn't work on pre-Lollipop even with AppCompat library. Only colorButtonNormal from theme works on pre-Lollipop.

这是真的吗?真可惜

我正在使用这个带有按钮的可绘制对象(是一种简单的绿色),但是当我使用按钮时发现它比普通按钮高。

这是文件btn_green.xml

<?xml version="1.0" encoding="utf-8"?>

<item android:state_pressed="true"><shape>
        <solid android:color="#ff5722" />

        <corners android:radius="4dp" />
    </shape></item>
<item android:state_focused="true"><shape android:shape="rectangle">
        <solid android:color="#4caf50" />

        <corners android:radius="4dip" />

        <stroke android:width="1dip" android:color="#F0FC00" />
    </shape></item>
<item><shape>
        <solid android:color="#4caf50" />

        <corners android:radius="4dp" />
    </shape></item>

按钮是这样的:

            <android.support.v7.widget.AppCompatButton
            android:id="@+id/btnIngresar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/btn_green"
            android:text="Hello"
            android:textStyle="bold" />

当我使用drawable时,按钮比普通按钮(没有drawable)高,我不知道为什么。

有一种方法可以在棒棒糖之前的设备中设置 BackgroundTint。试试这个: button.setSupportBackgroundTintList(getResources().getColorStateList(R.color.accentColor)); 参考这个答案:

如果你想要下面的风格

将此样式添加到您的按钮

style="@style/Widget.AppCompat.Button.Borderless.Colored"

如果你想要这种风格

添加以下代码

style="@style/Widget.AppCompat.Button.Colored"