首页 > 解决方案 > AndroidX在按钮上设置背景颜色没有效果

问题描述

我通常使用 android:background 或以编程方式使用 setBackgroundColor 在 XML 中设置按钮的背景颜色,但现在使用 AndroidX 库(我假设......),这些对按钮颜色没有影响

似乎设置 android:backgroundTint 有效,但这仅适用于 API 21 及更高版本

我怎样才能做到这一点?

例子:

这不应该产生一个带有红色背景按钮的布局......

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:background="#af2222"/>

</LinearLayout>

标签: androidandroidx

解决方案


我在 android x 中使用它而不是<Button背景颜色会改变

<androidx.appcompat.widget.AppCompatButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:background="@color/colorAccent"
        android:text="Done"
/>

推荐阅读