【问题标题】:How to make compose AlertDialog Rtl?如何编写 AlertDialog Rtl?
【发布时间】:2023-01-01 00:36:17
【问题描述】:

如何根据设备区域设置更改可组合项的方向,例如 AlertDialog 的内容?

【问题讨论】:

    标签: android android-jetpack-compose


    【解决方案1】:

    您可以像这样更改LayoutDirection

        CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl ) {
            ...
        }
    

    您可以根据区域设置更改LayoutDirection.Rtl vs LayoutDirection.Ltr,您可以从

    LocalContext.current.resources.configuration.locales
    

    【讨论】:

    • 谢谢用户496854!我使用你的答案,但对我的答案做了一些改动。你的回答没有完成。我在我的回答中完成了它。
    【解决方案2】:

    使用此功能:

    @Composable
    fun AlertDialogWithDirection(
        onDismissRequest: () -> Unit,
        confirmButton: @Composable () -> Unit,
        modifier: Modifier = Modifier,
        dismissButton: @Composable (() -> Unit)? = null,
        title: @Composable (() -> Unit)? = null,
        text: @Composable (() -> Unit)? = null,
        shape: Shape = MaterialTheme.shapes.medium,
        backgroundColor: Color = MaterialTheme.colors.surface,
        contentColor: Color = androidx.compose.material.contentColorFor(backgroundColor),
        properties: DialogProperties = DialogProperties()
    ) {
        val layoutDirection = LocalLayoutDirection.current
        AlertDialog(
            onDismissRequest,
            confirmButton = {
                CompositionLocalProvider(LocalLayoutDirection provides layoutDirection) {
                    confirmButton()
                }
            },
            modifier,
            dismissButton = {
                if (dismissButton != null)
                    CompositionLocalProvider(LocalLayoutDirection provides layoutDirection) {
                        dismissButton()
                    }
            },
            title = {
                if (title != null)
                    CompositionLocalProvider(LocalLayoutDirection provides layoutDirection) { title() }
            },
            text = {
                if (text != null)
                    CompositionLocalProvider(LocalLayoutDirection provides layoutDirection) { text() }
            },
            shape,
            backgroundColor,
            contentColor,
            properties
        )
    }
    

    并像这样使用:

    AlertDialogWithDirection(
        onDismissRequest = { 
    
        },
        title = {
            Text(
                text = "Your Text"
            )
        },
        text = {
            Text(
                text = "Your Text"
            )
        },
        confirmButton = {
            Button(
                onClick = {
                    ok()
                }
            ) {
                Text(
                    text = "Button"
                )
            }
        },
        dismissButton = {
            OutlinedButton(
                onClick = { 
                }
            ) {
                Text(text = "Dismiss")
            }
        }
    )
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-04
      • 2011-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多