Android Button设置backgroundTint及圆角后背景仍为默认问题求助
解决Android按钮圆角不生效的问题
问题根源
你同时设置了android:background(自定义圆角drawable)和android:backgroundTint,在Android 5.0及以上版本,backgroundTint会优先覆盖自定义背景的颜色和样式,导致圆角效果被默认样式覆盖。另外如果你的项目使用了Material Design主题,默认的Button其实是MaterialButton,它的默认样式也会干扰自定义背景。
解决方案
方案1:移除backgroundTint属性
直接删除布局里的android:backgroundTint="@color/our_lightGray",因为你已经在roundstyle.xml里设置了背景颜色,不需要重复用tint。修改后的Button代码:
<Button android:id="@+id/currentstatusButton" android:layout_width="329dp" android:layout_height="98dp" android:background="@drawable/roundstyle" android:text="Current Status" android:textColor="@color/black" tools:layout_editor_absoluteX="16dp" tools:layout_editor_absoluteY="460dp" />
方案2:禁用背景着色(保留tint属性时用)
如果一定要保留backgroundTint或者项目依赖Material组件,可添加app:backgroundTint="@null"(需要在布局根节点引入Material命名空间xmlns:app="http://schemas.android.com/apk/res-auto"),强制禁用tint对自定义背景的覆盖:
<Button android:id="@+id/currentstatusButton" android:layout_width="329dp" android:layout_height="98dp" android:background="@drawable/roundstyle" app:backgroundTint="@null" android:text="Current Status" android:textColor="@color/black" tools:layout_editor_absoluteX="16dp" tools:layout_editor_absoluteY="460dp" />
方案3:显式使用普通Button(针对Material主题)
如果你的App主题继承了Theme.MaterialComponents.*,默认的Button是MaterialButton,它会强制应用自身的样式。可以在布局里显式指定使用原生的android.widget.Button:
<android.widget.Button android:id="@+id/currentstatusButton" android:layout_width="329dp" android:layout_height="98dp" android:background="@drawable/roundstyle" android:text="Current Status" android:textColor="@color/black" tools:layout_editor_absoluteX="16dp" tools:layout_editor_absoluteY="460dp" />
验证
修改后重新编译运行,按钮应该会显示你定义的灰色圆角样式。
内容的提问来源于stack exchange,提问作者Akira
相关产品推荐
相关产品推荐

