如何以编程方式获取 Android 导航栏的高度和宽度?

问题描述:

屏幕底部的黑色导航栏在 Android 中不容易移除.自 3.0 以来,它一直是 Android 的一部分,作为硬件按钮的替代品.这是一张图片:

The black navigation bar on the bottom of the screen is not easily removable in Android. It has been part of Android since 3.0 as a replacement for hardware buttons. Here is a picture:

如何获得此 UI 元素的宽度和高度的大小(以像素为单位)?

How can I get the size of the width and the height of this UI element in pixels?

试试下面的代码:

Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
    return resources.getDimensionPixelSize(resourceId);
}
return 0;