美文网首页Android精选Android开发Android开发经验谈
Android 关于导航栏(虚拟按键)遮挡PopupWindow

Android 关于导航栏(虚拟按键)遮挡PopupWindow

作者: stone呀 | 来源:发表于2018-02-01 16:03 被阅读508次

    我们自定义popupWindow的时候,一般会设置这些参数

    setContentView(contentView);
    //设置高度为屏幕高度
    setWidth(UIUtils.getScreenHeight(context));
    //设置宽度为屏幕宽度
    setHeight(UIUtils.getScreenWidth(context));
    setFocusable(true);
    //dismiss时将背景透明度恢复为1f
    setOnDismissListener(()-> modifyActivityAlpha(1f));
    setBackgroundDrawable(new BitmapDrawable());
    setAnimationStyle(R.style.action_sheet_no_animation);
    //将透明度alpha值设为0.5f
    modifyActivityAlpha(0.5f);
    //在底部展示
    showAtLocation(context.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
    
    //改变背景透明度
    private void modifyActivityAlpha(float alpha) {
        WindowManager.LayoutParams params = context.getWindow().getAttributes();
        params.alpha = alpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(params);
     }
    
    关于获取屏幕的宽高可以查看我的这篇文章:https://www.jianshu.com/p/cbba19018f5d

    在魅族的手机(没有底部导航栏)上很开心很愉快的运行着,但是当我看到华为手机(有底部虚拟按钮)的显示结果时我是很头疼的,popupWindow有一部分布局被虚拟按钮遮挡了。

    图一(可以看到底部滑不上去,一部分被遮挡了。)
    one.png
    • 在经过一番google后发现大家一致推荐的解决方案,在参数设置中加上
    setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    
    • 满怀期待的等待着运行的结果,但是发现并没有解决问题,我开始思考哪里出问题了。
      后来发现是宽高设置的问题,我把宽高的设置改为:
    setWidth(WindowManager.LayoutParams.MATCH_PARENT);
    setHeight(WindowManager.LayoutParams.MATCH_PARENT);
    
    • 因为之前设置的是屏幕的宽高,是把虚拟按键的高度算在内的。
    • 终于解决了有虚拟按键时全屏的popupWindow出现的遮挡问题。
    图二(问题解决)
    image.png
    下面是最终的部分代码
     public void init(View contentView, View layoutContent) {
            setContentView(contentView);
            setWidth(WindowManager.LayoutParams.MATCH_PARENT);
            setHeight(WindowManager.LayoutParams.MATCH_PARENT);
            setFocusable(true);
            setOnDismissListener(()-> modifyActivityAlpha(1f));
            setBackgroundDrawable(new BitmapDrawable());
            setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
            setAnimationStyle(R.style.action_sheet_no_animation);
            modifyActivityAlpha(0.5f);
            showAtLocation(context.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
        }
    

    补充:在测试中仅仅使用了6.0的手机,有博主指出在7.0以上手机上会出现无效的问题,大家可以参考:http://blog.csdn.net/qinyunying/article/details/55051193
    如果解决了你的问题,希望能给个👍哦。

    相关文章

      网友评论

        本文标题:Android 关于导航栏(虚拟按键)遮挡PopupWindow

        本文链接:https://www.haomeiwen.com/subject/sgudzxtx.html