[英]background color of Android Button is not changing
The background color of the button won't change.按钮的背景颜色不会改变。
Here is the code:这是代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/purple_500">
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="5dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:background="@color/white"/>
</androidx.constraintlayout.widget.ConstraintLayout>
This is what the layout and blueprint looks like:这是布局和蓝图的样子:
I can't add Simple comment sorry about that so here is my solutions;我不能添加简单的评论抱歉,所以这是我的解决方案; You can use backgroundTint attribute instead of background or you can add custom style for button https://stackoverflow.com/a/31858629/13447094
您可以使用 backgroundTint 属性而不是背景,也可以为按钮https://stackoverflow.com/a/31858629/13447094添加自定义样式
Instead of android:background
use app:backgroundTint
.而不是
android:background
使用app:backgroundTint
。
This will make your code look like this:这将使您的代码如下所示:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/purple_500">
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="5dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:backgroundTint="@color/white"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Another way is ThemeOverlay, you can make a ThemeOverlay like this:另一种方法是 ThemeOverlay,您可以像这样制作 ThemeOverlay:
<style name="ThemeOverlay.Button.Red" parent="MaterialButtonStyle">
<item name="colorOnPrimary">@color/white</item>
<item name="colorPrimary">@color/red</item>
</style>
and then接着
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/purple_500">
<com.google.android.material.button.MaterialButton
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="5dp"
android:theme="@style/ThemeOverlay.Button.Red"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
colorPrimary =background of the button colorPrimary =按钮的背景
colorOnPrimary = textColor of the button colorOnPrimary = 按钮的文本颜色
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.