Android导航栏大小以xml为单位

fra*_*gon 23 android android-layout android-xml

有没有办法在xml文件中获取android中的导航栏大小?

 android:paddingTop="?android:attr/actionBarSize"
Run Code Online (Sandbox Code Playgroud)

哪里actionBarSize是导航栏大小?

Gaë*_* M. 24

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

@android:dimen/navigation_bar_height
Run Code Online (Sandbox Code Playgroud)

还有其他尺寸链接到导航栏,来自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>
Run Code Online (Sandbox Code Playgroud)

  • `错误:(167,39)资源不公开.(在'layout_marginBottom'中,值为'@ android:dimen/navigation_bar_height'). (31认同)

Max*_*Max 7

试试这个代码

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

  • OP 在 XML 中特别要求。 (2认同)