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

Jetpack Compose Dialog默认居底而非居中?三星Android13适配求助

解决Jetpack Compose Dialog在三星Android13设备底部对齐的问题

针对三星Galaxy Android13设备上Jetpack Compose Dialog组件默认显示在屏幕底部的适配问题,以下是几个可靠的解决方式:


方案1:外层Box强制内容居中

在Dialog的内容区域外层添加一个占满全屏的Box,并设置内容居中对齐,直接强制Dialog的布局容器居中:

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            YourAppTheme {
                Dialog(onDismissRequest = { /*TODO*/ }) {
                    Box(
                        modifier = Modifier.fillMaxSize(),
                        contentAlignment = Alignment.Center
                    ) {
                        Column(
                            modifier = Modifier
                                .fillMaxWidth()
                                .background(Color.White)
                                .padding(16.dp),
                            verticalArrangement = Arrangement.Center,
                            horizontalAlignment = Alignment.CenterHorizontally
                        ) {
                            Text(
                                text = "Title",
                                style = MaterialTheme.typography.titleSmall,
                                color = Color.Black
                            )
                            Spacer(modifier = Modifier.height(4.dp))
                            Text(
                                text = "Desc",
                                style = MaterialTheme.typography.bodySmall,
                                color = Color.Black
                            )
                        }
                    }
                }
            }
        }
    }
}

方案2:使用AlertDialog替代基础Dialog

AlertDialog组件内置了居中布局的逻辑,能绕过部分厂商的系统适配差异,直接替换原Dialog即可:

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            YourAppTheme {
                AlertDialog(
                    onDismissRequest = { /*TODO*/ },
                    confirmButton = {}, // 空按钮以保留原有布局样式
                    title = {
                        Text(
                            text = "Title",
                            style = MaterialTheme.typography.titleSmall,
                            color = Color.Black
                        )
                    },
                    text = {
                        Column {
                            Spacer(modifier = Modifier.height(4.dp))
                            Text(
                                text = "Desc",
                                style = MaterialTheme.typography.bodySmall,
                                color = Color.Black
                            )
                        }
                    },
                    containerColor = Color.White,
                    titleContentColor = Color.Black,
                    textContentColor = Color.Black
                )
            }
        }
    }
}

方案3:通过Window属性强制居中

直接获取Dialog对应的Window实例,设置窗口重力为居中,从系统层级修正布局:

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            YourAppTheme {
                val context = LocalContext.current
                Dialog(onDismissRequest = { /*TODO*/ }) {
                    val window = (context as ComponentActivity).window
                    window.setGravity(Gravity.CENTER)
                    window.attributes = window.attributes.apply {
                        width = WindowManager.LayoutParams.MATCH_PARENT
                        height = WindowManager.LayoutParams.WRAP_CONTENT
                    }

                    Column(
                        modifier = Modifier
                            .fillMaxWidth()
                            .background(Color.White)
                            .padding(16.dp),
                        verticalArrangement = Arrangement.Center,
                        horizontalAlignment = Alignment.CenterHorizontally
                    ) {
                        Text(
                            text = "Title",
                            style = MaterialTheme.typography.titleSmall,
                            color = Color.Black
                        )
                        Spacer(modifier = Modifier.height(4.dp))
                        Text(
                            text = "Desc",
                            style = MaterialTheme.typography.bodySmall,
                            color = Color.Black
                        )
                    }
                }
            }
        }
    }
}

以上方案均能解决该适配问题,优先推荐方案1或2,实现更简洁且符合Compose的声明式布局风格。

问题截图:
dialog-bug

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 21:37:11