当前位置: 移动技术网 > IT编程>移动开发>Android > 解决PopupWindow的阴影覆盖问题

解决PopupWindow的阴影覆盖问题

2019年06月01日  | 移动技术网IT编程  | 我要评论

黄和祥个人资料,虐杀原形 音乐 下载,打死不同台:冯巩死对头竟是他

版权声明:本文为xing_star原创文章,转载请注明出处!

本文同步自

popupwindow阴影覆盖问题

最近这段时间需求不是很多,就有时间解决下之前遗留的小尾巴,比如popwindow在某些特殊手机上阴影无法覆盖底部,看起来总觉得怪怪的,最近用全面屏手机相对较多,看到这种情况就更加显眼了,强迫症的我立刻动手fix这个问题。只记得之前是popwindow是在android n以上设备显示有问题,记得还是一两年前吧,那个时候还没怎么有全面屏手机的问题。在网上搜了下关于处理全面屏显示popwindow的问题,找到了想要的答案。

本文以一个完整的sample,描述popwindow在红米6pro这样的全面屏手机上的使用。

案例描述:

创建一个basepopwindow类,作为popwindow的基类。

public abstract class basepopwindow extends popupwindow {
    private final view content;
    protected activity context;
 
    public basepopwindow(activity context) {
        super(context);
        this.context = context;
        layoutinflater inflater = (layoutinflater) context
                .getsystemservice(context.layout_inflater_service);
        content = inflater.inflate(getlayoutid(), null);
        this.setcontentview(content);
        this.setwidth(viewgroup.layoutparams.match_parent);
        this.setheight(viewgroup.layoutparams.match_parent);
        this.setfocusable(true);
        this.setoutsidetouchable(true);
        this.update();
        colordrawable dw = new colordrawable(color.parsecolor("#80000000"));
        this.setbackgrounddrawable(dw);
        findallview(content);
        initlistener();
    }
 
    protected abstract void findallview(view view);
 
    protected abstract int getlayoutid();
 
    protected abstract void initlistener();
}

写一个简单的业务popwindow,这里只是演示,就弄的比较简单。

public class custompopwindow extends basepopwindow {
    public custompopwindow(activity context) {
        super(context);
    }
 
    @override
    protected void findallview(view view) {
 
    }
 
    @override
    protected int getlayoutid() {
        return r.layout.popwindow_select;
    }
 
    @override
    protected void initlistener() {
 
    }
 
    public void showpopupwindow(view anchor) {
        if (!this.isshowing()) {
            show(anchor);
        } else {
            this.dismiss();
        }
    }
 
    private void show(view anchor) {
        if (build.version.sdk_int >= 24) {
            rect rect = new rect();
            anchor.getglobalvisiblerect(rect);
            int height;
            if (screenutils.isshownavbar(context)) {
                height = anchor.getresources().getdisplaymetrics().heightpixels - rect.bottom;
            } else {
                height = screenutils.getrealheight(context) - rect.bottom;
            }
            setheight(height);
        }
        this.showasdropdown(anchor, 0, 0);
    }
 
}

我们只关注show方法,里面特意判断了是android n以上的设备,另外又针对android n以上设备区分了是否是显示了导航栏,这个说法也是别人写的,我的理解是通过计算屏幕高度,真实高度,来判断应该给popwindow设置多少的高度。这块为何要按照这个逻辑写,我并没有深入研究源码,先满足需求,有时间有条件在深究了。

在mainactivity中响应页面事件。

public class mainactivity extends appcompatactivity {
    textview textview;
 
    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_main);
        textview = findviewbyid(r.id.text_view);
        textview.setonclicklistener(new view.onclicklistener() {
            @override
            public void onclick(view v) {
                custompopwindow popwindow = new custompopwindow(mainactivity.this);
                popwindow.showpopupwindow(v);
            }
        });
    }
}

还有部分代码未贴出,在源码中给出,。

其他资料参考:

popupwindow在android7.0及以上位置和阴影显示问题 

github开源项目basepopup 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网