英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论