我拥有一台可以隐藏导航栏的HTC One A9。在我的应用程序中,我需要获取导航栏的高度并设置与其对应的填充。这就是我现在的问题:当我隐藏导航栏时,填充仍然被设置(即使Snapchat有这个问题)。我的问题是:是否有替代代码使其有效?
public static int getNavBarHeight(Context context) {
int result = 0;
int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
感谢您的帮助!
答案 0 :(得分:8)
这是我用来获取导航栏大小的代码。它的高度将在Point.y
归功于this answer
public static Point getNavigationBarSize(Context context) {
Point appUsableSize = getAppUsableScreenSize(context);
Point realScreenSize = getRealScreenSize(context);
// navigation bar on the right
if (appUsableSize.x < realScreenSize.x) {
return new Point(realScreenSize.x - appUsableSize.x, appUsableSize.y);
}
// navigation bar at the bottom
if (appUsableSize.y < realScreenSize.y) {
return new Point(appUsableSize.x, realScreenSize.y - appUsableSize.y);
}
// navigation bar is not present
return new Point();
}
public static Point getAppUsableScreenSize(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
return size;
}
public static Point getRealScreenSize(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
if (Build.VERSION.SDK_INT >= 17) {
display.getRealSize(size);
} else if (Build.VERSION.SDK_INT >= 14) {
try {
size.x = (Integer) Display.class.getMethod("getRawWidth").invoke(display);
size.y = (Integer) Display.class.getMethod("getRawHeight").invoke(display);
} catch (IllegalAccessException e) {} catch (InvocationTargetException e) {} catch (NoSuchMethodException e) {}
}
return size;
}
编辑:要回答你的问题,我必须使用此功能,因为我想将ResideMenu添加到我的应用程序,但由于导航栏,我的应用程序底部结束了一个奇怪的空边距。
所以我编辑了ResideMenu添加的这个函数,如下所示:
@Override
protected boolean fitSystemWindows(Rect insets) {
// Applies the content insets to the view's padding, consuming that content (modifying the insets to be 0),
// and returning true. This behavior is off by default and can be enabled through setFitsSystemWindows(boolean)
// in API14+ devices.
int bottomPadding = insets.bottom;
Point p = getNavigationBarSize(getContext());
if (Build.VERSION.SDK_INT >= 21 && p.x != 0) {
Resources resources = getContext().getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
bottomPadding += resources.getDimensionPixelSize(resourceId);
}
}
this.setPadding(viewActivity.getPaddingLeft() + insets.left, viewActivity.getPaddingTop() + insets.top,
viewActivity.getPaddingRight() + insets.right, viewActivity.getPaddingBottom() + bottomPadding);
insets.left = insets.top = insets.right = insets.bottom = 0;
return true;
}
希望能帮助你。
答案 1 :(得分:1)
使用以下代码获取导航栏。但是,您需要考虑多窗口模式以及导航栏是在窗口的底部还是左侧或右侧。
getNavigationBarHeight(){
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId);
}
return 0;
}
答案 2 :(得分:1)
这是最简单的答案。您只需要传递活动的 rootView。如果不显示条形,高度将为 0。
View rootView;
ViewCompat.setOnApplyWindowInsetsListener(rootView, (v, insets) -> {
final int statusBarHeight = insets.getInsets(WindowInsetsCompat.Type.statusBars()).top; // in px
final int navigationBarHeight = insets.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom; // in px
// do something with the heights
return WindowInsetsCompat.CONSUMED;
});
答案 3 :(得分:0)
fun Context.isSoftNavigationBarAvailable(): Boolean {
val navBarInteractionModeId = resources.getIdentifier(
"config_navBarInteractionMode",
"integer",
"android"
)
if (navBarInteractionModeId > 0 && resources.getInteger(navBarInteractionModeId) > 0) {
// nav gesture is enabled in the settings
return false
}
val appUsableScreenSize = Point()
val realScreenSize = Point()
val defaultDisplay = (getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay
defaultDisplay.getSize(appUsableScreenSize)
defaultDisplay.getRealSize(realScreenSize)
return appUsableScreenSize.y < realScreenSize.y }