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

使用Jetpack Compose时LocalContentAlpha.current的作用域疑问

问题与验证

我运行以下代码(Code A)来实现同一图标显示不同颜色的效果,通过日志(Result A)获取LocalContentAlpha.current的取值。我的推测是:不同位置的LocalContentAlpha.current会返回不同值,且日志中Enabled对应的该值作用域是IconButton,现在想确认日志中Outside对应的该值作用域是否为Row()。

Code A

@Composable
fun RecordTitleWithToolBar(   
){ 
     Row() {

        Log.e("my", "Outside "+LocalContentAlpha.current.toString() )

        IconButton(
            enabled = true,
            onClick = {}
        ) {
            DisplayIcon(icon=Icons.Filled.PlayArrow)
            Log.e("my", "Enabled "+LocalContentAlpha.current.toString() )  
        }

        IconButton(
            enabled = false,
            onClick = {}
        ) {
            DisplayIcon(icon=Icons.Filled.PlayArrow)
            Log.e("my", "Disabled "+LocalContentAlpha.current.toString() ) 
        }
    }
}

@Composable
fun DisplayIcon(
    icon: ImageVector,
    dim:  Dp = 24.dp,
    tint: Color = Color.Blue
) {
    Icon(icon, null, modifier = Modifier.size(dim), tint = tint.copy(LocalContentAlpha.current))
}

Result A

2022-08-28 21:02:59.384 26068-26068/info.dodata.soundmeter E/my: Enabled 0.87
2022-08-28 21:02:59.387 26068-26068/info.dodata.soundmeter E/my: Disabled 0.38
2022-08-28 21:02:59.466 26068-26068/info.dodata.soundmeter E/my: Outside 0.87
2022-08-28 21:02:59.471 26068-26068/info.dodata.soundmeter E/my: Enabled 0.87
2022-08-28 21:02:59.475 26068-26068/info.dodata.soundmeter E/my: Disabled 0.38
2022-08-28 21:02:59.480 26068-26068/info.dodata.soundmeter E/my: Outside 0.87
2022-08-28 21:02:59.485 26068-26068/info.dodata.soundmeter E/my: Enabled 0.87
2022-08-28 21:02:59.487 26068-26068/info.dodata.soundmeter E/my: Disabled 0.38

结论

从日志结果能确认,Outside对应的LocalContentAlpha.current值作用域就是Row():

  • Row()没有显式设置LocalContentAlpha,所以它继承的是上层未被覆盖的默认值0.87(对应ContentAlpha.medium);
  • IconButton会根据自身enabled状态自动覆盖LocalContentAlpha:启用时为0.87,禁用时为0.38,这和日志里的Enabled、Disabled结果完全对应,也验证了不同作用域下LocalContentAlpha.current的取值差异。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 07:54:23