Android虚拟键(NavigationBar)适配问题

华为以及nexus一些机型的NavigationBar存在遮挡App页面的问题

1、遮挡布局 布局内加上android:fitsSystemWindows="true" 就可以了。

网上还有一种办法是

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);

getWindow().getDecorView().setSystemUiVisibility(View.STATUS_BAR_HIDDEN);

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_SHOW_FULLSCREEN);

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

这种办法一开始是会隐藏,但是点击布局,navicationBar又出现了。

2、遮挡控件(比如底部弹窗)

这个时候我们可以采用下面这种办法来解决

public staticPointgetNavigationBarSize(Context context) {
Point appUsableSize =getAppUsableScreenSize(context);
Point realScreenSize =getRealScreenSize(context);
// navigation bar on the right
if(appUsableSize.x< realScreenSize.x) {
return newPoint(realScreenSize.x- appUsableSize.x,appUsableSize.y);
}
// navigation bar at the bottom
if(appUsableSize.y< realScreenSize.y) {
return newPoint(appUsableSize.x,realScreenSize.y- appUsableSize.y);
}
// navigation bar is not present
return newPoint();
}
public staticPointgetAppUsableScreenSize(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size =newPoint();
display.getSize(size);
return size;
}
public staticPointgetRealScreenSize(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size =newPoint();
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;
}

你可能感兴趣的:(Android虚拟键(NavigationBar)适配问题)