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

huangapple go评论112阅读模式
英文:

How to get AppBar height programmatically?

问题

我正在编写一个移动应用程序,需要找出如何获取“应用栏”的高度(在不同设备上可能会有所变化)。我进行了大量搜索,但没有找到任何答案。
我已经使用以下代码来获取“操作栏”的高度:

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

我认为也许获取操作栏的方法类似,但是没有任何效果。
只是为了明确,对我来说,应用栏就是这样的:

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

而它在不同设备上可能是这样的:(这就是我为什么需要一个动态值)

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

提前感谢您的帮助!

英文:

I'm coding a mobile application and I need to find how to get the "appbar" height (which can change from a device to another). I searched a lot but couldn't find any answer.
I already did this to get the "action bar" height :

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

I thought that maybe it would be similar for the action bar, but nothing worked.
Just to be clear, to me the app bar is that :

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

And it can be that depending on the device : (That's why I need a dynamic value)

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

Thank you in advance!

答案1

得分: 1

第一张图片甚至不是应用程序的一部分,而是系统界面。可以从渲染的组件中获取值,但系统界面的状态栏高度相对不重要。

那个标有“Accueil”的区域应该是 ?android:attr/actionBarSizeandroid.R.attr.actionBarSize,默认为 56dp

英文:

The first one picture isn't even part of the application, but it's the system UI. One could obtain the value from the rendered component, but the system UI's status-bar's height is rather irrelevant.

The area, where it reads "Accueil", should be ?android:attr/actionBarSize or android.R.attr.actionBarSize, which defaults to 56dp.

答案2

得分: 1

这部分被称为状态栏,通常高度为24dp,但在像那样的刘海屏幕上,它至少与刘海的高度相同:

> 在纵向方向上没有设置特殊标志的情况下,状态栏必须至少延伸到刘海的高度。

这里有关于处理刘海屏幕的指导 - 基本上,你可能不需要过多担心高度。默认情况下,你可以假装它不存在,但如果你需要控制并在其中显示自己的内容,有一些标志可以设置。如果你确实需要这个值:

> 避免硬编码状态栏的高度,因为这可能导致内容重叠或被截断。在可能的情况下,使用WindowInsetsCompat来检索状态栏的高度,并确定应用于内容的适当填充。

英文:

That part's called the status bar it's usually 24dp high, but with notched diplays like that one it's at least the height of the notch:

> 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 - basically you probably shouldn't be worrying about the height at all. By default you can just pretend it's not there, if you need to control it and display your own content in there, there are flags you can set. If you do need the value:

> 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.

huangapple
  • 本文由 发表于 2020年9月30日 22:17:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64139545.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定