有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何以编程方式获取AppBar高度?

我正在编写一个移动应用程序,我需要找到如何获得“appbar”高度(可以从一个设备更改为另一个设备)。我找了很多,但找不到任何答案。 我已经这样做了,以获得“动作栏”的高度:

val resources: Resources = this.resources
        val resourceId: Int = resources.getIdentifier("navigation_bar_height", "dimen", "安卓")
        val lengthNavBar = if (resourceId > 0) {
            resources.getDimensionPixelSize(resourceId)
        } else 0

我想也许动作条也会类似,但没有任何效果。 我只想说清楚,对我来说,应用程序栏是:

enter image description here

这可能取决于设备:(这就是为什么我需要一个动态值)

enter image description here

提前谢谢你


共 (2) 个答案

  1. # 1 楼答案

    第一张图片甚至不是应用程序的一部分,但它是系统UI。可以从呈现的组件获取值,但系统UI的状态栏高度与此无关

    读取“Accueil”的区域应为?android:attr/actionBarSizeandroid.R.attr.actionBarSize,默认为56dp

  2. # 2 楼答案

    该部分称为状态栏,它是usually 24dp high,但对于像这样的凹口凹陷,它至少是凹口的高度:

    In portrait orientation with no special flags set, the status bar must extend to at least the height of the cutout.

    Here's the guidance on dealing with cutouts-基本上你根本不应该担心身高。默认情况下,您可以假装它不存在,如果您需要控制它并在其中显示您自己的内容,可以设置一些标志。如果您确实需要该值:

    Avoid hard-coding the status bar height, as this can lead to overlapping or cut-off content. Where possible, use WindowInsetsCompat to retrieve the status bar height and determine the appropriate padding to apply to your content.