在三星上,Compose AlertDialog 始终采用全宽

Jok*_*nas 10 android android-alertdialog android-jetpack-compose

在我的配备 One UI 5.0 和 Android 13 的三星 Galaxy S22+ 上,撰写 AlertDialog 始终占据全宽,在其他设备上它的工作方式与预期一致。

Compose版本是1.3.1

您只需从 Google Play 商店下载材料目录应用程序即可重现此内容。

我怀疑这很可能是 Compose 方面的一个错误,如果有快速修复,我将不胜感激。

@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")
                }
            }
        )
    }
}

Run Code Online (Sandbox Code Playgroud)

更新:

创建了一个问题,这似乎是三星方面的一个错误: https ://issuetracker.google.com/issues/260755409

pra*_*ala 6

在我的设备上更新 Android 13 后,带有 XML 布局的对话框采用了预期的宽度。但 Compose AlertDialog&Dialog占据了全宽。我们仅在使用撰写对话框时面临这个问题,

我使用的是配备One UI 5.0Android 13的Samsung Galaxy M32,应用程序使用Compose 版本 1.1.0-beta01和33,targetSdkVersion

使用usePlatformDefaultWidth = true没有帮助,

这个问题很可能是 Compose 端的一个错误,您可以在 compose 中找到 Dialog 和 AlertDialog 的快速修复,

  1. 对于撰写 AlertDialog()

我使用了修饰符并设置DialogProperty usePlatformDefaultWidth为 false 并设置了fillMaxWidth分数 0.92f。

modifier = Modifier.fillMaxWidth(0.92f),
properties =DialogProperties(usePlatformDefaultWidth =false),
Run Code Online (Sandbox Code Playgroud)

编写 AlertDialog() 代码片段:

AlertDialog(
    modifier = Modifier.fillMaxWidth(0.92f),
    properties = DialogProperties(
        usePlatformDefaultWidth = false
    ),
    onDismissRequest = { ... },
    buttons = {
        Column(
            modifier = Modifier
                .fillMaxWidth()
        ) {
            ...
        }
    },
    title = {
        
    },
    text = {
        Column(
            modifier = Modifier
                .fillMaxWidth()
        ) {
            ........
        }
    }
)
Run Code Online (Sandbox Code Playgroud)
  1. 对于撰写对话框()

我曾经用、 半径、设置为背景颜色并设置为 false 来Surface包装对话框内容modifier = Modifier.fillMaxWidth(0.92f)RoundedCornerShapeColor.TransparentDialogProperty usePlatformDefaultWidth

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

Compose Dialog() 代码片段:

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
                ) {
                    ........
                }
            })
    })
Run Code Online (Sandbox Code Playgroud)


m.r*_*ter 1

Alert-Dialog-Composable 接受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
    ...
)
Run Code Online (Sandbox Code Playgroud)

默认情况下,usePlatformDefaultWidth = true,因此对话框不应填充屏幕宽度。

-> 您看到的很可能是一个错误并且应该报告