android 编写AlertDialog在Samsung上始终采用全宽度

gorkyyrv  于 2023-01-28  发布在  Android
关注(0)|答案(2)|浏览(159)

在我的三星Galaxy S22+与一个UI 5.0和Android 13,组成AlertDialog总是占用全宽,在其他设备上,它的工作正如预期。
合成版本为1.3.1
您可以通过下载material catalog app from Google Play store来重现这个过程。
我怀疑这很可能是一个错误的组成方面,如果有一个快速修复,我会很感激。

@Composable
fun AlertDialogSample() {
    val openDialog = remember { mutableStateOf(true) }

    if (openDialog.value) {
        AlertDialog(
            onDismissRequest = {
                // Dismiss the dialog when the user clicks outside the dialog or on the back
                // button. If you want to disable that functionality, simply use an empty
                // onCloseRequest.
                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")
                }
            }
        )
    }
}

fhity93d

fhity93d1#

可组合警报对话框接受DialogProperties

@Composable
fun AlertDialog(
    properties: DialogProperties = DialogProperties()
    ...
)

/**
 * Properties used to customize the behavior of a [Dialog].      
   ...
 * @property usePlatformDefaultWidth Whether the width of the dialog's content should
 * be limited to the platform default, which is smaller than the screen width.
 */
class DialogProperties @ExperimentalComposeUiApi constructor(
    val usePlatformDefaultWidth: Boolean = true
    ...
)

默认为usePlatformDefaultWidth = true,因此对话框不应填满屏幕宽度。

  • 〉您看到的很可能是一个bug,应该报告
lnlaulya

lnlaulya2#

在我的设备上更新Android 13后,带有XML布局的对话框占用了预期宽度。但编写AlertDialogDialog占用了全宽。我们仅在编写对话框时遇到此问题,
我正在使用三星Galaxy M32配备One UI 5.0Android 13**,应用程序使用编写版本1.1.0-beta 01targetSdkVersion 33,
使用usePlatformDefaultWidth = true没有帮助,
此问题很可能是编写端的错误,您可以在编写中找到Dialog和AlertDialog的快速修复程序,
1.用于编写警报对话框()
我使用了修饰符,并将DialogPropertyusePlatformDefaultWidth设置为false,并将fillMaxWidth设置为小数0.92f。

modifier = Modifier.fillMaxWidth(0.92f),
properties =DialogProperties(usePlatformDefaultWidth =false),

编写AlertDialog()代码片段:

AlertDialog(
    modifier = Modifier.fillMaxWidth(0.92f),
    properties = DialogProperties(
        usePlatformDefaultWidth = false
    ),
    onDismissRequest = { ... },
    buttons = {
        Column(
            modifier = Modifier
                .fillMaxWidth()
        ) {
            ...
        }
    },
    title = {
        
    },
    text = {
        Column(
            modifier = Modifier
                .fillMaxWidth()
        ) {
            ........
        }
    }
)

1.用于合成对话框()
我已经使用Surface将对话框内容 Package 为modifier = Modifier.fillMaxWidth(0.92f)、带半径的RoundedCornerShape,将Color.Transparent设置为背景色**,并将DialogProperty usePlatformDefaultWidth设置为false**

Surface(
        modifier = Modifier.fillMaxWidth(0.92f),
        shape = RoundedCornerShape(8.dp),
        color = Color.Transparent,
        content = {})

编写对话框()代码片段:

Dialog(
    onDismissRequest = { },
    properties = DialogProperties(
        dismissOnClickOutside = true,
        dismissOnBackPress = true,
        usePlatformDefaultWidth = false
    ),
    content = {
        Surface(
            modifier = Modifier.fillMaxWidth(0.92f),
            shape = RoundedCornerShape(8.dp),
            color = Color.Transparent,
            content = {
                Column(
                    modifier = Modifier
                        .background(color = colorResource(id = android.R.color.white))
                        .fillMaxWidth(1f)
                        .wrapContentHeight(),
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.Center
                ) {
                    ........
                }
            })
    })

相关问题