Android 关于导航栏(虚拟按键)遮挡PopupWindow底部布局的问题

 

我们自定义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
如果解决了你的问题,希望能给个?哦。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值