Android Studio中安卓应用按钮颜色无法修改问题求助
你遇到的问题是因为当前Android Studio创建的新项目默认使用Material Components主题,该主题下Button控件默认会使用backgroundTint属性覆盖android:background设置的背景,所以你设置的白色shape背景不生效,始终显示主题默认的蓝色。
可行解决方案
方案1:禁用背景着色让自定义背景生效
在你的activity_to_do.xml的Button标签中添加app:backgroundTint="@null"属性即可,修改后的Button代码如下:
<Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:background="@drawable/button" app:backgroundTint="@null" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
方案2:替换兼容控件
将原生Button替换为AppCompatButton,该控件默认支持自定义背景,不需要额外设置tint属性:
<androidx.appcompat.widget.AppCompatButton android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:background="@drawable/button" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
适配优化建议
你当前button.xml中圆角使用的单位是px,不同分辨率设备显示效果差异较大,建议替换为dp单位适配不同机型:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="24dp"/> <solid android:color="@color/white"/> </shape>
内容的提问来源于stack exchange,提问作者ruwan liyanage
相关产品推荐
相关产品推荐

