android - 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>
解决方案
我在 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"
/>
推荐阅读
- android - 为什么我不能使用我的代码保存到外部存储?
- android - 为什么我无法解析 JSON?
- python - 在xml中查找特定的孩子
- guice - Guice 注入在 Web 应用程序中不起作用
- python - Django 分析测试
- arrays - Matlab中的组件索引
- google-api - 如何将用户输入存储在对话框流中的参数中
- codenameone - 我在哪里可以找到 CodenameOne 的 iKVM 版本?
- vba - 尝试向用户发送从内容控制下拉列表中选择的选项
- kubernetes - 如何在没有 googlecloud SDK 的情况下访问 Google Kubernetes 集群?