当前位置: 首页 > news >正文

Android 中解决 Button 按钮背景色设置无效的问题

1、问题描述

  • 在布局文件中有两个 Button 按钮,为每个按钮设置不同的背景色,但是显示出来的效果都是紫色的,跟设置的颜色不同,布局文件如下所示:
	<Buttonandroid:id="@+id/button_cancel"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="@dimen/sp_30"android:textColor="@color/white"android:text="@string/cancel"android:background="@color/blue"app:layout_constraintTop_toTopOf="parent"app:layout_constraintLeft_toLeftOf="parent"><Buttonandroid:id="@+id/button_confirm"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="@dimen/sp_30"android:textColor="@color/white"android:text="@string/confirm"android:background="@color/red"app:layout_constraintTop_toTopOf="@+id/button_cancel"app:layout_constraintLeft_toRightOf="@+id/button_cancel"android:layout_marginStart="@dimen/dp_50"/>
  • 正常效果应该是一个显示蓝色,一个显示红色,但是实际效果是两个都显示紫色,如下所示:
    在这里插入图片描述

2、原因分析

  • 这种现象是由于程序中设置了新版本主题,而新版本主题会导致按钮颜色显示异常。
    <style name="MyTheme.FullScreen" parent="Theme.MaterialComponents.DayNight.NoActionBar"><item name="android:windowFullscreen">true</item><item name="android:windowActionBar">false</item><item name="android:windowNoTitle">true</item></style>
  • 这里用了 Material 新版本主题 Theme.MaterialComponents.DayNight.NoActionBar。

3、解决方法

  • 通过修改主题可以解决该问题,修改 app/res/values 目录下的 themes.xml 文件,将父主题更改为 Theme.MaterialComponents.DayNight.NoActionBar.Bridge,成功解决了所有按钮颜色统一显示为紫色的问题。
    <style name="MyTheme.FullScreen" parent="Theme.MaterialComponents.DayNight.NoActionBar.Bridge"><item name="android:windowFullscreen">true</item><item name="android:windowActionBar">false</item><item name="android:windowNoTitle">true</item></style>
  • 显示效果
    在这里插入图片描述

4、两个主题区别

  • Theme.MaterialComponents.DayNight.NoActionBar
    这是一个标准的 Material Components 主题,支持日夜模式切换(DayNight),并且不包含 ActionBar。适用于需要完全采用 Material Components 设计语言的应用程序,且不需要 ActionBar 的场景。
  • Theme.MaterialComponents.DayNight.NoActionBar.Bridge
    这是一个过渡主题,用于在迁移到 Material Components 时,保持与旧主题的兼容性。适用于从旧的主题(如 Theme.AppCompat)迁移到 Material Components 主题时,但又不想立即完全替换所有主题属性,可以使用这个桥接主题。
http://www.lryc.cn/news/614131.html

相关文章:

  • BGP笔记及综合实验
  • 如何在simulink中双击一个模块弹出一个exe?
  • 三防平板+天通卫星电话,打通无人之境的通信经脉
  • 前端开发:JavaScript(7)—— Web API
  • 从手工到智能决策,ERP让制造外贸企业告别“数据孤岛“降本增效
  • 生产管理ERP系统|物联及生产管理ERP系统|基于SprinBoot+vue的制造装备物联及生产管理ERP系统设计与实现(源码+数据库+文档)
  • Selenium + Python + Pytest + Yaml + POM
  • ISL9V3040D3ST-F085C一款安森美 ON生产的汽车点火IGBT模块,绝缘栅双极型晶体管ISL9V3040D3ST汽车点火电路中的线圈驱动器
  • 【量子计算】量子计算驱动AI跃迁:2025年算法革命的曙光
  • 行业速览:中国新能源汽车市场格局与关键趋势
  • 时序数据库-涛思数据库
  • 实现一个进程池(精讲)
  • ​​Vue3 + Element Plus 构建的现代化即时通讯在线客服系统​
  • STM32学习笔记5-TIM定时器-1
  • 线程池基础知识
  • wstool和catkin_tools工具介绍
  • 智慧社区(十)——声明式日志记录与小区地图功能实现
  • Python实现点云PCA配准——粗配准
  • Ubuntu安装 L20显卡驱动
  • Linux网络--2、Socket编程
  • 中国电信清华:大模型驱动的具身智能发展与挑战综述
  • 动漫软件集合分享
  • Pytest项目_day08(setup、teardown前置后置操作)
  • 144.二叉树的前序遍历
  • 鲸签云解决互联网行业合同管理难题​
  • 【Rust】多级目录模块化集成测试——以Cucumber为例
  • 线程组和线程池的基本用法
  • 【Spring Boot 快速入门】八、登录认证
  • duxapp 2025-05-29 更新 兼容鸿蒙C-API方案,现在鸿蒙端可以用于生产
  • React SSR 水合问题