Android 开发问题:全局的主题颜色设置,导致 CheckBox 控件在勾选状态下不显示样式

xml 复制代码
<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.AutomaticDeviceDispenser" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/white</item>
        <item name="colorPrimaryVariant">@color/white</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/white</item>
        <item name="colorSecondaryVariant">@color/white</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
        <item name="windowNoTitle">true</item>
    </style>
</resources>
xml 复制代码
<android.widget.CheckBox
    android:id="@+id/cb_append_newline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:text="补 \\r\\n" />
问题原因
  • 在 Android 开发中,在应用上述主题的情况下,CheckBox 控件在勾选状态下不显示样式
  1. 上述主题将几乎所有颜色都设置为了白色,该项目中 CheckBox 使用了 colorSecondary 做为勾选框的颜色

  2. 而背景也因为是白色或浅色,所以白色的勾选框在白色背景上就像是不显示一样

处理策略
  • 在 CheckBox 上添加 app:buttonTint 属性,覆盖全局的主题颜色设置
xml 复制代码
<android.widget.CheckBox
    android:id="@+id/cb_append_newline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:buttonTint="@color/black"
    android:checked="true"
    android:text="补 \\r\\n" />
相关推荐
charlie11451419122 分钟前
现代C++工程实践 WeakPtr 实战(三):WeakPtrFactory 与「最后成员」惯用法
开发语言·c++·开源项目·现代c++
布吉岛的石头1 小时前
Java 程序员第 45 阶段11:网关统一路由大模型接口,配合 Nacos 配置治理,灰度路由:基于Nacos元数据实现大模型服务灰度发布与权重路由
java·服务发现
我命由我123451 小时前
Android 开发问题:Cannot resolve method ‘repeat‘ in ‘String‘
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
IT小盘2 小时前
04-大模型流式输出原理-SSE与Python实现
开发语言·网络·人工智能·python
baozhengw2 小时前
信创改造-SpringBootJar包改为WAR包部署Tomcat
java·tomcat
不如语冰2 小时前
AI大模型入门-Python进阶-上下文管理与with语句
开发语言·数据结构·数据库·人工智能·pytorch·redis·python
M1A12 小时前
基于 Spring AOP 构建系统操作日志:从注解设计到异步落库的完整实战
java
光头闪亮亮2 小时前
Fyne ( go跨平台GUI )项目实战-scanner摄像头扫码组件开发技术详解
android·go
RainCity2 小时前
Java Swing 自定义组件库分享(十五)
java·笔记·后端
farerboy2 小时前
23-Java 构造函数
java·后端