关于Android中Button的背景色无法修改的问题的解决方法

关于Android中Button的背景色无法修改的问题的解决方法

问题描述

在修改Button的背景色时,始终无法修改,并且默认是蓝紫色。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:foreground="@mipmap/ic_launcher"
    android:foregroundGravity="left">

    <Button
        android:layout_width="300dp"
        android:layout_height="450dp"
        android:text="按钮1"
        android:background="#00FF00"
        />

    <Button
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:text="按钮2"
        android:textColor="@color/red"
        android:layout_gravity="center"
        />

</FrameLayout>

我这里把按钮1的背景色设置为绿色,但是实际效果还是蓝紫色。

image-20210902121918873

解决方法

找到app/res/values目录中的themes.xml文件,把样式改为其它样式。

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

修改为

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

注意:name属性值写你自己的应用名称,即无需改变name属性值,只需要改变parent属性值即可。

这样就可以设置按钮的背景色了。

image-20210902123622251

更多内容欢迎来访个人博客
个人B站

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值