【发布时间】:2021-10-25 09:58:18
【问题描述】:
我想知道如何使用 Jetpack Compose 获取屏幕宽度和高度?
无论如何你可以检索屏幕尺寸以外
getWindowManager().getDefaultDisplay()?
提前谢谢...
【问题讨论】:
我想知道如何使用 Jetpack Compose 获取屏幕宽度和高度?
无论如何你可以检索屏幕尺寸以外
getWindowManager().getDefaultDisplay()?
提前谢谢...
【问题讨论】:
其他解决方法包括:-
A.) 在层次结构的最高级别声明 BoxWithConstraints,然后访问在框范围内公开的 maxHeight 和等效宽度属性
B.) 使用自定义布局
Layout(
content = { ... }
){ measurables, constraints ->
//Exposes constraints.maxWidth, and a height equivalent
}
【讨论】:
您可以通过LocalConfiguration.current 实现此目的:
@Composable
fun PostView() {
val configuration = LocalConfiguration.current
val screenHeight = configuration.screenHeightDp.dp
val screenWidth = configuration.screenWidthDp.dp
...
}
【讨论】: