【问题标题】:screen width and height in jetpack composejetpack compose 中的屏幕宽度和高度
【发布时间】:2021-10-25 09:58:18
【问题描述】:

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

提前谢谢...

【问题讨论】:

    标签: android-jetpack-compose


    【解决方案1】:

    其他解决方法包括:-

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

    B.) 使用自定义布局

    Layout(
     content = { ... }
    ){ measurables, constraints ->
     //Exposes constraints.maxWidth, and a height equivalent
    }
    

    【讨论】:

      【解决方案2】:

      您可以通过LocalConfiguration.current 实现此目的:

      @Composable
      fun PostView() {
        val configuration = LocalConfiguration.current
      
        val screenHeight = configuration.screenHeightDp.dp
        val screenWidth = configuration.screenWidthDp.dp
      
        ...
      
      }
      
      

      【讨论】:

      • 谢谢!像魅力一样工作。
      猜你喜欢
      • 2012-11-01
      • 2023-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-02
      • 1970-01-01
      • 2022-10-25
      • 2011-09-09
      相关资源
      最近更新 更多