TypechoJoeTheme

香草物语

统计
登录
用户名
密码
/
注册
用户名
邮箱
输入密码
确认密码
文章目录

android Button修改背景色

Laughing博主
2020-12-05
/
0 评论
/
1,480 阅读
/
129 个字
/
百度已收录
12/05
本文最后更新于2024年03月18日,已超过185天没有更新。如果文章内容或图片资源失效,请留言反馈,我会及时处理,谢谢!

问题描述

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

比如如下定义一个椭圆形的按钮。

样式btn_bg_red.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/teal_200"/>
    <corners android:topLeftRadius="10dip"
        android:topRightRadius="10dip"
        android:bottomRightRadius="10dip"
        android:bottomLeftRadius="10dip" />
    <!--圆角矩形白色背景-->
</shape>

MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dip"
        android:layout_weight="1"
        android:background="@drawable/btn_bg_red"
        android:textColor="@color/purple_200"
        android:gravity="center"
        android:text="圆角Button"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

可以看到,虽然修改了背景色,但是根本没起作用。

解决

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

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