导航栏遮挡webview问题(适配华为,小米方案)

目前发生这种情况的主要有两种手机1.华为,2.小米

1.华为适配

 /**
     * 判断底部navigator是否已经显示   适配华为
     * @param windowManager
     * @return
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private boolean hasSoftKeys(WindowManager windowManager){
        Display d = windowManager.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;
    }

   /**
     * 得到屏幕的高度
     */
    public static int getDeviceHeight(Context context) {

        return context.getResources().getDisplayMetrics().heightPixels;
    }

 

项目中的使用:

    if (hasSoftKeys(activity.getWindowManager())) {
         ViewGroup.LayoutParams params = web_layout.getLayoutParams();
         params.height = getDeviceHeight(activity);
         web_layout.setLayoutParams(params);       
       }  
  /**
     * 得到屏幕的高度
     */
    public static int getDeviceHeight(Context context) {

        return context.getResources().getDisplayMetrics().heightPixels;
    }
           

通过hasSoftKeys方法可以判断华为手机是否有虚拟导航栏,若有则动态修改webview的父布局的高度

2.小米适配

小米适配主要通过

boolean flag = Settings.Global.getInt(activity.getContentResolver(), "force_fsg_nav_bar", 0) != 0;

这个方法判断是否有虚拟导航栏 ,flag为true则没有,为false则有

项目中使用:

//判断是否是小米手机 
if (PermissionManager.getInstance().checkPermissionForXiaomi(ContextWrap.of(activity))){
            boolean flag = Settings.Global.getInt(activity.getContentResolver(), "force_fsg_nav_bar", 0) != 0;
            if (!flag){
                //有导航栏
                setWebContainerLayoutParams(web_layout,activity);
            }
        }
  /**
     * 此处用于在处理存在底部导航栏的设备上存在导航栏遮挡html页面的问题
     */
    private void setWebContainerLayoutParams(View view,Activity activity) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        ViewGroup.MarginLayoutParams marginParams = null;
        //获取view的margin设置参数
        if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
            marginParams = (ViewGroup.MarginLayoutParams) layoutParams;
        } else {
            //不存在时创建一个新的参数
            marginParams = new ViewGroup.MarginLayoutParams(layoutParams);
        }
        marginParams.setMargins(0, 0, 0, ScreenUtil.getSystemNavigationBarHeight(activity));
        view.setLayoutParams(marginParams);
    }

暂时未发现其他机型有这个问题,也就先适配到这里

你可能感兴趣的:(android)