You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Compose自定义按钮重组后文字变白问题(红米Note8深色模式)

问题原因及解决方法

问题原因

该问题的核心是深色模式下Text组件未明确绑定目标颜色,重组时被系统/主题的默认文字色覆盖。红米Note8的深色主题适配逻辑存在特殊处理:

  • 首次渲染时,Button的contentColor可正确传递给Text;
  • 加载状态切换完成重组后,Text未强制锁定颜色,导致系统深色主题的白色文字色替代了你指定的黑色。

解决方法

以下两种方案可直接修复问题:

方案一:给Text明确指定颜色

在Text组件中直接设置color参数,锁定目标黑色:

Text(
    text = textButton,
    fontSize = 17.sp,
    color = Color(0xFF15151A) // 强制指定需要的文字颜色
)

方案二:稳定Button的颜色配置

将Button的颜色配置提取为稳定变量,避免重组时重新创建配置导致的颜色传递异常:

// 提取为组件内或全局的稳定颜色配置
private val customButtonColors = ButtonDefaults.buttonColors(
    containerColor = Color.White,
    contentColor = Color(0xFF15151A),
    disabledContainerColor = Color.White.copy(alpha = 0.8f),
    disabledContentColor = Color(0xFF15151A)
)

@Composable
fun CustomButton(
    modifier: Modifier,
    textButton: String,
    isLoading: Boolean = false,
    onClick: () -> Unit
) {
    if (isLoading) {
        Box(
            modifier = modifier
                .background(Color.White.copy(alpha = 0.8f), shape = RoundedCornerShape(15.dp)),
            contentAlignment = Alignment.Center
        ) {
            CircularProgressIndicator(
                modifier = Modifier.size(30.dp),
                color = Color(0xFF15151A)
            )
        }
    } else {
        Button(
            modifier = modifier,
            onClick = onClick,
            colors = customButtonColors, // 使用预定义的稳定配置
            shape = RoundedCornerShape(size = 15.dp),
        ) {
            Text(
                text = textButton,
                fontSize = 17.sp
            )
        }
    }
}

额外排查点

检查App全局主题是否在深色模式下对Button/Text有颜色覆盖(比如主题中定义了textButtonColor等全局属性),这类全局配置的优先级会高于组件内的局部设置。

内容的提问来源于stack exchange,提问作者fabian rojas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 15:52:35