Android开发中Button背景颜色不能修改问题及解决方法

目录

        问题:

        问题原因:

        解决方法:


        在Android中,Button是一种按钮组件,用户能够在该组件上点击,并引发相应的事件处理函数。       

         在进行Android开发的时候,都需要使用到按钮,但是对于初学者来说,刚开始的按钮都是默认的主题颜色,不管怎么修改都变不了颜色,在此记录一下踩过的坑。

问题:

使用Android Studio进行android开发时,不管是拖出来的Button,还是自己设置的Button,Button的背景色一直无法修改,呈现系统默认的紫色。

以一个Button举例,代码:

<Button
        android:id="@+id/button4"
        android:layout_width="143dp"
        android:layout_height="80dp"
        android:background="@drawable/shapge_1"
        android:text="Button"
        tools:layout_editor_absoluteX="160dp"
        tools:layout_editor_absoluteY="317dp" />

db64f78de7c04334a46c288566d8f311.png

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="50dp"/>
    <gradient android:startColor="#ff0000"
        android:centerColor="#00ff00"
        android:endColor="#0000ff"
        android:angle="0"/>

</shape>

2c401816ee2744348868c9f9e7d3d0e4.png

 预览按钮的时候应该是彩色,但还是默认的颜色:

c6f0ae07928443cc8a7a1dec8f93d769.png

问题原因:

        出现该问题的原因主要是因为使用Android Studio 4.1之后的版本进行开发时,创建的项目默认的主题所有Button都是Material类型的Button,默认使用主题色,所以想要修改颜色,就要把默认主题给关了或替代了。

解决方法:

方式一:

<Button
        android:id="@+id/button4"

 

改为-------->

 

<android.widget.Button
        android:id="@+id/button4"

<Button
        android:id="@+id/button4"

改为-------->

<android.widget.Button
        android:id="@+id/button4"

方式二:

找到temes.xml文件

将这段代码:
<style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">


修改为:
---------->
<style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">

修改完毕之后Button背景颜色出现:

62098a717b3f4721bf507b3b3889b8db.png

 

  • 48
    点赞
  • 107
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏志121

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值