【无标题】

关于Android中Button的Backgroud背景设置默认为蓝紫色,且无法修改的问题
面向单身的编程 2020-11-18 21:40:13 8977 收藏 92
分类专栏: Android学习 文章标签: android android studio
版权
Android学习
专栏收录该内容
2 篇文章 0 订阅
订阅专栏
问题简述:

在修改Button的背景颜色时,始终无法修改颜色为设置的颜色,且颜色始终为默认的蓝紫色。

    <Button
        android:id="@+id/btn_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_1"
        android:backgroundTint="@null"
        android:textSize="20sp"
        android:textColor="#FFFFFF"
        android:background="#FF0011"></Button>

    <Button
        android:id="@+id/btn_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_2"
        android:textSize="20sp"
        android:textColor="#FFFFFF"
        android:backgroundTint="@null"
        android:background="@drawable/bg_btn2"
        android:layout_below="@id/btn_1"
        android:layout_marginTop="20dp"></Button>

    <Button
        android:id="@+id/btn_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮3"
        android:textSize="20sp"
        android:backgroundTint="@null"
        android:textColor="#FF9900"
        android:background="@drawable/bg_btn3"
        android:layout_below="@id/btn_2"
        android:layout_marginTop="20dp"></Button>

    <Button
        android:id="@+id/btn_4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮4"
        android:textSize="20sp"
        android:backgroundTint="@null"
        android:textColor="#FFFFFF"
        android:background="@drawable/bg_btn4"
        android:layout_below="@id/btn_3"
        android:layout_marginTop="20dp"></Button>

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45

上文中drawable/bg_btn等颜色为非默认蓝紫色。
实际模拟机效果(真机效果一样)如下:
在这里插入图片描述
问题解决:

博主的解决方法:
默认的颜色设置来自于res/values/themes.xml与夜间模式(应该是)下的res/values-night/themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.HelloWorld" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
</resources>

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16

将(或你选择的其它主题)

 <style name="Theme.HelloWorld" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

    1

修改为(或其它能够实现非默认颜色的主题)

<style name="Theme.HelloWorld" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">

    1

即可解决问题,修改后的效果图如下:
在这里插入图片描述

当然,经过搜索,还得到了以下解决方法,虽然没有解决我的问题,但是希望能够帮到你:
1、给按钮增加一个属性app:backgroundTint="@null"
2、点击菜单中的 “baiFile” -> “Invalidate Caches / Restart”,然后点击对话框中的 “Invalidate and Restart”,清空 cache 并且重启。
3、更换旧版本(或许确实是版本问题)
4、将themes文件下的<style> 中的所有默认属性注释掉(亲测有效,或许这才是正确方法)
问题总结:

虽然不知道原理是什么,但是在搜索问题的时候很少人遇到这种问题,且遇到问题的多半是近期提问。总结来说应该是更新问题导致的,或者说因为我选择了空白项目导致使用了默认的themes。
————————————————
版权声明:本文为CSDN博主「面向单身的编程」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_43417003/article/details/109786563

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值