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" />
相关推荐
apcipot_rain26 分钟前
C++转java日记(13)
java·开发语言
郝学胜-神的一滴1 小时前
Effective Python 条款41:试Mix‑In组合式开发
开发语言·pytorch·python·pycharm·numpy
吃我一窝jio1 小时前
Python与Redis集合深度解析
开发语言·redis·笔记·python
意念回复2 小时前
Transformer 系列(五):从 Block 到模型——三大架构、因果掩码与自回归生成
android·回归·transformer
星恒随风2 小时前
从0实现C++ AI大模型接入SDK(五):DeepSeekProvider的实现——策略模式、HTTP请求与SSE流式响应
linux·开发语言·c++·人工智能
CoderYanger2 小时前
Java EE 进阶:12.Spring AOP(第二弹)
java·前端·数据库·spring boot·spring·java-ee·mybatis
饮水桶_T3 小时前
Git笔记
开发语言·笔记·算法
国科安芯3 小时前
让每一瓦热都听话:航天器主动热控系统CANFD控制网络的可靠性设计
开发语言·网络·单片机·嵌入式硬件·信息与通信·抗辐射·载荷
CoderYanger3 小时前
Java EE 进阶:12.Spring AOP(第一弹)
java·开发语言·spring boot·后端·spring·java-ee·交互
松仔log3 小时前
Kotlin中级——KMP
android·开发语言·kotlin