美文网首页DialogAndroid开发Android技术知识
使用popupwindow的坑(不显示)

使用popupwindow的坑(不显示)

作者: laer_L | 来源:发表于2016-10-09 23:16 被阅读6794次
很多时候你可能会遇到popupwindow不显示或是一部分手机能显示一些不能显示的情况,那恭喜你看到了这篇文章

<br />

最开始我创建popupwindow的方式

代码如下

    //肉眼看上去没什么问题,果然拿出我的小米note跑起来也是正常的,但是偏偏来个三星和魅族的一些手机就显示不了
    View view = LayoutInflater.from(this).inflate(R.layout.choice_rider_num, null);
        window = new PopupWindow(this);
        window.setContentView(view);
        window.setOutsideTouchable(false);
        window.setFocusable(true);
        // 实例化一个ColorDrawable颜色为半透明
        window.setBackgroundDrawable(null);
        window.setAnimationStyle(R.style.mypopwindow_anim_style);
        window.showAtLocation(v, Gravity.BOTTOM, 0, 0);

仔细研究后发现是没有给popupwindow设置宽高导致的,于是

 window = new PopupWindow(this);
        window.setContentView(view);
        //设置宽高
        window.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        window.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        window.setOutsideTouchable(false);
        window.setFocusable(true);
        window.setAnimationStyle(R.style.mypopwindow_anim_style);
        window.showAtLocation(v, Gravity.BOTTOM, 0, 0);

解决啦

同理你也可以


  window = new PopupWindow(v, WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT);
        window.setContentView(view)
        window.setOutsideTouchable(false);
        window.setFocusable(true);
        // 实例化一个ColorDrawable颜色为半透明
        window.setBackgroundDrawable(null);
        window.setAnimationStyle(R.style.mypopwindow_anim_style);
        window.showAtLocation(v, Gravity.BOTTOM, 0, 0);

相关文章

网友评论

  • 没风的地方:我的就诡异了,我的是设置了宽高还是有的显示有的不显示
  • degrty:确实是popwindow的宽高没有设置
  • 3ed297765c63:多谢 哈哈哈
    laer_L: @小火车_污污污 共同进步

本文标题:使用popupwindow的坑(不显示)

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