xml中的Android导航栏大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25603718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:33:57  来源:igfitidea点击:

Android navigation bar size in xml

androidandroid-layoutandroid-xml

提问by fragon

Is there any way to get the size of navigation bar in android in xml file similar to this?

有什么方法可以在与此类似的xml文件中获取android中导航栏的大小吗?

 android:paddingTop="?android:attr/actionBarSize"

where actionBarSize is navigation bar size?

actionBarSize 是导航栏大小?

回答by Ga?tan M.

After looking in android source it looks like there is dimens for navigation bar height :

在查看 android 源代码后,它看起来导航栏高度有尺寸:

@android:dimen/navigation_bar_height

Thereare other dimens linked to nav bar, examples from android values/dimens.xml :

还有其他维度链接到导航栏,来自 android values/dimens.xml 的示例:

<!-- Height of the bottom navigation / system bar. -->
<dimen name="navigation_bar_height">48dp</dimen>
<!-- Height of the bottom navigation bar in portrait; often the same as @dimen/navigation_bar_height -->
<dimen name="navigation_bar_height_landscape">48dp</dimen>
<!-- Width of the navigation bar when it is placed vertically on the screen -->
<dimen name="navigation_bar_width">42dp</dimen>

回答by Max

Try this code:

试试这个代码

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

回答by mayank1513

As suggested in many of similar questions, for example this, this, this, and this, simply getting navigation bar height may not be enough. We need to consider whether 1. navigation bar exists, 2. is it on the bottom, or right or left, 3. is app open in multi-window mode.

正如许多类似问题中所建议的那样,例如thisthisthisthis,仅仅获取导航栏高度可能是不够的。我们需要考虑1.导航栏是否存在,2.是在底部,还是在右边或左边,3.应用程序是否在多窗口模式下打开。

There is a simple one line solution

有一个简单的单行解决方案

android:fitsSystemWindows="true"

or programatically

或以编程方式

findViewById(R.id.your_root_view).setFitsSystemWindows(true);

you may also get root view by

您也可以通过以下方式获得根视图

findViewById(android.R.id.content).getRootView();
or
getWindow().getDecorView().findViewById(android.R.id.content)

For more details on getting root-view refer - https://stackoverflow.com/a/4488149/9640177

有关获取根视图的更多详细信息,请参阅 - https://stackoverflow.com/a/4488149/9640177