关于Android中Button的Backgroud背景设置默认为蓝紫色,且无法修改的问题

问题简述:

在修改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>

上文中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>

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

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

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

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

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

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

问题总结:

虽然不知道原理是什么,但是在搜索问题的时候很少人遇到这种问题,且遇到问题的多半是近期提问。总结来说应该是更新问题导致的,或者说因为我选择了空白项目导致使用了默认的themes。

Android开发中,添加箭头到背景通常是为了指示某个特定的UI元素,例如按钮或者导航条。这可以通过使用图片资源或者使用XML绘图来实现。以下是一个简单的例子,说明如何使用XML创建带有箭头的背景: 首先,你可以创建一个shape drawable资源文件。以下是一个XML示例,描述了如何创建一个带有向右箭头的背景: ```xml <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 背景层 --> <item> <shape android:shape="rectangle"> <solid android:color="@color/your_background_color" /> <!-- 可以添加更多属性,如边框、圆角等 --> </shape> </item> <!-- 箭头层 --> <item android:top="16dp" android:bottom="16dp" android:end="16dp"> <rotate android:fromDegrees="45" android:toDegrees="45" android:pivotX="0%" android:pivotY="50%"> <shape android:shape="rectangle"> <solid android:color="@color/your_arrow_color" /> <!-- 可以通过调整pivotX, fromDegrees, toDegrees来改变箭头方向和大小 --> </shape> </rotate> </item> </layer-list> ``` 在上面的代码中,`your_background_color` 和 `your_arrow_color` 需要替换成你希望使用的颜色值。这个例子中,`rotate` 标签用于创建箭头形状,并可以通过调整角度来改变箭头的方向。 你将这个XML文件保存到`res/drawable`目录下,并在你的布局文件中引用它作为某个组件的背景,例如: ```xml <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Next" android:background="@drawable/your_arrow_background" /> ``` 这样,按钮就会显示出带有箭头的背景
评论 32
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值