需求
页面处于全屏展示时候,有虚拟按键的情况,例如华为手机,索尼手机等。底部虚拟按键会遮挡内容,如何做统一的处理,显得尤为重要
工具类下载地址http://download.csdn.net/download/gl_mine_csdn/10183783
思考
其实底部虚拟的设计初衷,是为了省去底部物理按键的位置,为手机屏幕提供更多的控件,故采用在屏幕上使用虚拟按键来处理屏幕事件。
方案
- 监听整个屏幕状态
mView.getRoot().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
}
});
- 是否存在虚拟按键
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static boolean isHaveSoftKey(Activity activity) {
Display d = activity.getWindowManager().getDefaultDisplay();
DisplayMetrics realDisplayMetrics = new DisplayMetrics();
d.getRealMetrics(realDisplayMetrics);
int realHeight = realDisplayMetrics.heightPixels;
int realWidth = realDisplayMetrics.widthPixels;
DisplayMetrics displayMetrics = new DisplayMetrics();
d.getMetrics(displayMetrics);
int displayHeight = displayMetrics.heightPixels;
int displayWidth = displayMetrics.widthPixels;
return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
}
3.获取底部虚拟按键的高度
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static int getBottomSoftKeysHeight(Activity activity) {
Display d = activity.getWindowManager().getDefaultDisplay();
DisplayMetrics realDisplayMetrics = new DisplayMetrics();
d.getRealMetrics(realDisplayMetrics);
int realHeight = realDisplayMetrics.heightPixels;
DisplayMetrics displayMetrics = new DisplayMetrics();
d.getMetrics(displayMetrics);
int displayHeight = displayMetrics.heightPixels;
return (realHeight - displayHeight);
}
4.重新渲染界面(修改布局显示)
if (isHaveSoftKey(this)) {
DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mViewContainer.getLayoutParams();
params.setMargins(0, 0, 0, ScreenUtils.getBottomSoftKeysHeight(this));
mViewContainer.setLayoutParams(params);
} else {
DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mViewContainer.getLayoutParams();
params.setMargins(0, 0, 0, 0);
mViewBinding.homeDrawCoordinatorCl.setLayoutParams(params);
}
总结
问题迎刃而解,如有问题请多指教。