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

Jetpack Compose阴影异常:如何移除TextField外的矩形背景?

解决TextField角落外侧矩形背景问题

问题出在你使用的shadow修饰符未指定形状,默认会生成矩形阴影,后续的background虽设置了圆角,但阴影的矩形边缘会在角落外侧显现出来。

修复步骤:

  • 给shadow修饰符添加shape参数,与background保持一致的RoundedCornerShape(10.dp)

修改后的代码:

@Composable
fun MyTextField() {
    var text by remember { mutableStateOf("") }

    Box(
        modifier = Modifier
            .padding(15.dp)
            .shadow(5.dp, shape = RoundedCornerShape(10.dp)) // 添加shape参数匹配圆角
            .background(color = Color.White, shape = RoundedCornerShape(10.dp))
            .fillMaxWidth()
            .height(50.dp),
        contentAlignment = Alignment.Center,
    ) {
        TextField(
            value = text,
            onValueChange = { text = it },
            label = { Text(text = "Phone number", color = Color.Gray, fontSize = 14.sp) },
            modifier = Modifier
                .fillMaxSize()
                .background(color = Color.Transparent, shape = RoundedCornerShape(10.dp)),
            colors = TextFieldDefaults.textFieldColors(
                backgroundColor = Color.Transparent,
                focusedIndicatorColor = Color.Transparent,
                unfocusedIndicatorColor = Color.Transparent
            ),
        )
    }
}

调整后阴影会跟随圆角形状生成,角落外侧的矩形背景就会消失。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 11:30:52