Compose修饰符未被遵守

huangapple go评论49阅读模式
英文:

Compose Modifiers not being honored

问题

以下是翻译好的部分:

我在表单的setContent块中通过when语句交换了一些Composeables。其中一个子Composeable的顶层部分是一个列(Column),水平对齐设置为Alignment.CenterHorizontally,垂直排列设置为Arrangement.Center。然而,当组合时,列的子项被放在左上角而不是中心。有没有办法修复这个问题?示例代码如下:

setContent {
    var status by remember { mutableStateOf(Status.State1) }
    when(status) {
        Status.State1 -> {
            // 背景颜色不会改变
            Surface(modifier = Modifier.background(Color(0xFF000000)))
        }
        Status.State2 -> {
            // 没有应用任何参数
            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center,
                modifier = Modifier.width(IntrinsicSize.Max)
            ) {
                Text(text = "我应该在中间")
            }
        }
        else -> {}
    }
}

希望这有助于解决你的问题。

英文:

I have some Composeables swapped by a when statement in the setContent block of a form. One of those child composeable's top level part is a column with horizontalAlignment set to Alignment.CenterHorizontally, and verticalArrangement set to Arrangement.Center. However, when composed, the column's children are put in the upper lefthand corner not the center. Any way to fix this? Example Code:

setContent {
    var status by remember { mutableStateOf(Status.State1) }
    when(status) {
        Status.State1 -> {
            //background color does not change
            Surface(modifier = Modifier.background(Color(0xFF000000)))
        }
        Status.State2 -> {
            //none of the parameters are applied
            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center,
                modifier = Modifier.width(IntrinsicSize.Max)
            ) {
                Text(text = "I should be in the center")
            }
        }
        else -> {}

</details>


# 答案1
**得分**: 0

```kotlin
setContent(ComposeView(requireContext()).apply {
    // 将主要的Composeable自动充满屏幕大小。
})

然而,setContent {} 方法不会自动充满屏幕大小。将 Modifier.fillMaxSize() 添加到主要的Composeable可以修复这个问题。


<details>
<summary>英文:</summary>

The method 
```kotlin
setContent(ComposeView(requireContext()).apply {
    //your composeable
})

automatically inflates the main composeable to max screen size. However, the the setContent {} method does not. Adding Modifier.fillMaxSize() to the main composeable fixes the issue.

huangapple
  • 本文由 发表于 2023年5月18日 00:30:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274292.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定