应用上传华为应用市场,结果被拒,原因是真人测试手机带虚拟按键,结果导致虚拟按键和屏幕某点击事件冲突,一点击就会退出到手机主页。废话不多说,直接上代码。
方案一:最简单的方法是布局文件根目录中添加 android:fitsSystemWindows=“false”,可以自动根据屏幕适配,但是我的项目用到了沉浸式状态栏,所以这个方法直接Pass掉. 方案二:调用: BaseActivity中: UIUtils.assistActivity(getRootView(this), this); private static View getRootView(Activity context) { return ((ViewGroup)context.findViewById(android.R.id.content)).getChildAt(0); }
UIUtils中:
/**
* 关联要监听的视图
*
* @param viewObserving
*/
public static void assistActivity(View viewObserving, Context context) {
new UIUtils(viewObserving, context);
}
private UIUtils(View viewObserving, Context context) {
mViewObserved = viewObserving;
//给View添加全局的布局监听器
mViewObserved.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
resetLayoutByUsableHeight(computeUsableHeight());
}
});
frameLayoutParams = mViewObserved.getLayoutParams();
this.context = context;
}
private void resetLayoutByUsableHeight(int usableHeightNow) {
//比较布局变化前后的View的可用高度
if (usableHeightNow != usableHeightPrevious) {
//如果两次高度不一致
//将当前的View的可用高度设置成View的实际高度
frameLayoutParams.height = usableHeightNow;
mViewObserved.requestLayout();//请求重新布局
usableHeightPrevious = usableHeightNow;
}
}
/**
* 计算视图可视高度
*
* @return
*/
private int computeUsableHeight() {
Rect r = new Rect();
mViewObserved.getWindowVisibleDisplayFrame(r);
//适配沉浸式状态栏 增加状态栏的高度
return (r.bottom - r.top + getStatusBarHeight1());
}
private int getStatusBarHeight1() {
try {
Class<?> c = Class.forName("com.android.internal.R$dimen");
Object obj = c.newInstance();
Field field = c.getField("status_bar_height");
int x = Integer.parseInt(field.get(obj).toString());
return context.getResources().getDimensionPixelSize(x);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
|