我有一个观点,我想在一个16x9的比例.
在肖像中,它看起来很好:
在横向中,它使用了错误的高度,因此文本没有居中:
(您可以滚动到查看视图的其余部分:
要复制的代码是:
/**
* Portrait: width=375.6dp height=211.4dp
* Landscape: expecting height=375.6dp but get height=443.1dp (a height that is taller than the screen is wide, not constrained?)
*/
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
RotationHeightDemoTheme {
// Column(modifier = Modifier.verticalScroll(rememberScrollState())) { // swapping out LazyColumn to Column with verticalScroll still gives the same issue
LazyColumn {
item {
BoxWithConstraints(
modifier = Modifier
.aspectRatio(16f / 9f, matchHeightConstraintsFirst = true)
// .fillParentMaxSize() // Using this "fixes" the issue in landscape but breaks portrait
.background(Color.Red),
contentAlignment = Alignment.Center,
) {
Text(text = "BOX $maxWidth / $maxHeight")
}
}
}
}
}
}
}
comments .fillParentMaxSize
个"修复"景观:
但却打破了肖像:
我需要保留LazyColumn,因为在肖像中,其他视图可能会出现在屏幕上,而在风景中是看不见的.
为什么在风景中BoxWith约束有错误的高度尺寸?