如何自定义AppCompatButton的颜色

8
我看到在这里有新的appCompat控件可用。我在Android应用程序中实现了它,但是我没有找到任何特定的方法来自定义其颜色。就像我们在样式中设置强调颜色一样,编辑文本会自动捕获它,但在AppCompatButton的情况下不起作用。有人发现相关内容吗?
2个回答

12

请看这里:如何使用 Material Design 和 AppCompat 来给 Android 按钮上色

总的来说,你可以在按钮本身上使用 tintBackground 属性,或者使用 colorControlNormal(或二者结合使用)。

另外,只要你正确地使用主题并从 AppCompatActivity 继承,你就可以直接使用 Button,它会自动转换为 AppCompatButton

来自上述链接的示例:

theme.xml:

<item name="colorButtonNormal">@color/button_color</item>

v21/theme.xml

<item name="android:colorButtonNormal">@color/button_color</item>

或者

<Button
       android:id="@+id/add_remove_button"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:backgroundTint="@color/bg_remove_btn_default"
       android:textColor="@android:color/white"
       tools:text="Remove" />

一个简单的答案解决了一个大问题。谢谢! - jlccaires

10

像这样使用 SupportLib 和 AppCompatButton:

<android.support.v7.widget.AppCompatButton
       android:id="@+id/add_remove_button"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       app:backgroundTint="@color/bg_remove_btn_default"
       android:textColor="@android:color/white"
       tools:text="Remove" />

应用程序是一个 mxlns: xmlns:app="http://schemas.android.com/apk/res-auto"

因此,背景色着色也适用于 preLollipop 版本。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接