11

我想知道如何使用 Jetpack Compose 获取屏幕宽度和高度?无论如何你可以检索屏幕尺寸以外的东西 getWindowManager().getDefaultDisplay()吗?

提前致谢...

4

2 回答 2

29

您可以通过以下方式实现LocalConfiguration.current

@Composable
fun PostView() {
  val configuration = LocalConfiguration.current

  val screenHeight = configuration.screenHeightDp.dp
  val screenWidth = configuration.screenWidthDp.dp

  ...

}

于 2021-08-25T09:02:22.997 回答
0

其他解决方法包括:-

BoxWithConstraintsA.)在层次结构的最高级别声明 a ,然后访问maxHeight在框范围内公开的和等效宽度属性

B.) 使用自定义布局

Layout(
 content = { ... }
){ measurables, constraints ->
 //Exposes constraints.maxWidth, and a height equivalent
}
于 2021-08-25T10:12:51.617 回答