有没有类似于这样在android的xml文件中获取导航栏大小的方法?
android:paddingTop="?android:attr/actionBarSize"
actionBarSize是导航栏的尺寸吗?
有没有类似于这样在android的xml文件中获取导航栏大小的方法?
android:paddingTop="?android:attr/actionBarSize"
actionBarSize是导航栏的尺寸吗?
在查看Android源代码后,似乎有导航栏高度的尺寸:
@android:dimen/navigation_bar_height
这里还有与导航栏相关的其他尺寸,例如来自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>
onCreate
中。 - Tilded试试这个代码:
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId);
}
return 0;
正如类似问题this,this,this和this中建议的那样,仅获取导航栏高度可能不足够。我们需要考虑以下几点:1. 导航栏是否存在,2. 是否在底部、右侧或左侧,3. 应用程序是否在多窗口模式下打开。
有一个简单的一行解决方案。
android:fitsSystemWindows="true"
findViewById(R.id.your_root_view).setFitsSystemWindows(true);
findViewById(android.R.id.content).getRootView();
or
getWindow().getDecorView().findViewById(android.R.id.content)
获取根视图的更多详细信息,请参考- https://dev59.com/Ym855IYBdhLWcg3wMBPV#4488149