【Android】关于Button背景色/样式设置失效

一、问题描述

在设置button背景颜色后,button背景依旧为蓝色。如下所示:

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="HelloWorld"
        android:textSize="50sp"
        android:background="@color/purple_200"
      />

【Android】关于Button背景色/样式设置失效

二、问题缘由 / 解决

在使用Android Studio 4.1+ 进行开发时,创建的项目默认的主题是Theme.MaterialComponents.DayNight.DarkActionBar
。所有Button都是Material类型的Button,默认使用主题色。

解决方法:

  1. 将主题切换为:Theme.AppCompat.*** , 不使用Material控件

<style name="Theme.ShapePractice" parent="Theme.AppCompat.DayNight.DarkActionBar">

  1. 设置 android:backgroundTint="@null" 着色设置为null后再进行背景设置

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:backgroundTint="@null"
        android:background="@color/purple_200"
        android:text="HelloWorld"
        android:textSize="50sp" />

参考

android – How to change the color of a button? – Stack Overflow

【Android】关于Button背景色/样式设置失效

© 版权声明
THE END
如果内容对您有所帮助,就支持一下吧!
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容