三星Galaxy S22+上Compose AlertDialog占满宽度问题求助
修复三星One UI 5.0设备上Compose AlertDialog占满屏幕宽度的问题
在搭载One UI 5.0与Android 13的三星Galaxy S22+设备上,使用Compose 1.3.1开发的AlertDialog会异常占满整个屏幕宽度,其他设备显示正常,该问题可通过Google Play的Material Catalog应用复现,且已确认是三星侧系统Bug。
以下是两种快速修复方案:
方案一:给AlertDialog添加宽度限制
通过外层Box组件限制对话框的最大宽度,符合Material Design的对话框宽度规范:
@Composable fun FixedAlertDialogSample() { val openDialog = remember { mutableStateOf(true) } // 遵循Material Design建议的手机端对话框宽度 val dialogMaxWidth = 320.dp if (openDialog.value) { Box(modifier = Modifier.fillMaxWidth().padding(horizontal = 24.dp)) { AlertDialog( onDismissRequest = { openDialog.value = false }, title = { Text(text = "Title") }, text = { Text( "This area typically contains the supportive text " + "which presents the details regarding the Dialog's purpose." ) }, confirmButton = { TextButton(onClick = { openDialog.value = false }) { Text("Confirm") } }, dismissButton = { TextButton(onClick = { openDialog.value = false }) { Text("Dismiss") } } ) } } }
方案二:自定义Dialog组件完全控制布局
使用基础Dialog组件封装自定义对话框,彻底规避系统对默认AlertDialog的异常影响:
@Composable fun CustomWidthDialogSample() { val openDialog = remember { mutableStateOf(true) } val dialogWidth = 320.dp if (openDialog.value) { Dialog(onDismissRequest = { openDialog.value = false }) { Surface( modifier = Modifier.width(dialogWidth), shape = MaterialTheme.shapes.medium, tonalElevation = 6.dp ) { Column(modifier = Modifier.padding(24.dp)) { Text( text = "Title", style = MaterialTheme.typography.titleLarge, modifier = Modifier.padding(bottom = 16.dp) ) Text( text = "This area typically contains the supportive text " + "which presents the details regarding the Dialog's purpose.", style = MaterialTheme.typography.bodyMedium ) Row( modifier = Modifier.fillMaxWidth().padding(top = 24.dp), horizontalArrangement = Arrangement.End ) { TextButton(onClick = { openDialog.value = false }) { Text("Dismiss") } TextButton(onClick = { openDialog.value = false }) { Text("Confirm") } } } } } } }
原理说明:两种方案均通过手动指定对话框宽度,覆盖三星系统对Compose AlertDialog布局的异常处理,确保对话框在所有设备上都遵循Material Design的视觉规范。
内容的提问来源于stack exchange,提问作者Jokubas Trinkunas
相关产品推荐
相关产品推荐

