PopupWindow封装

PopupWindow弹窗我们经常使用,但是如果一个项目里使用过多,那代码就会很多,所以我们需要封装
public class Pop {
    private static Context mContext;
    private final View contentview;

    //    Context context;
    public Pop(Builder b){

        contentview = LayoutInflater.from(mContext).inflate(b.contentviewid,null);



        //创建PopupWindow对象,指定宽度和高度
        PopupWindow  window = new PopupWindow(contentview, b.width, b.height);
        //  设置动画
//        window.setAnimationStyle(R.style.popup_window_anim);
        // 设置背景颜色
        window.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#F8F8F8")));
        // 设置可以获取焦点
        window.setFocusable(b.fouse);
        // 设置可以触摸弹出框以外的区域
        window.setOutsideTouchable(true);
//        window.setFocusableInTouchMode(true);
        window.setFocusable(true);

        // 更新popupwindow的状态
        window.update();
        //  以下拉的方式显示,并且可以设置显示的位置Gravity.BOTTOM  showAtLocation
//        window.showAsDropDown(Gravity.BOTTOM, 0, );
//        window.showAtLocation(BOTTOM,0,0,0);
        //        设置Popupwindow显示位置(从底部弹出)
//        window2.showAtLocation(findViewById(R.id.inposi_service_mode), Gravity.BOTTOM, 0, 0);
        window.showAtLocation(contentview, Gravity.CENTER, 0, 0);
        Log.e("-------","全都走完了,没问题啊");
//       popup.getpop(window);
    }
    //拿到View,用来找到popupwindow中的控件
public View initview(){
    return contentview;
}
//然后,需要用Builder类来将属性传入进去,不然直接传递,会显示不出来弹窗的
public static class Builder{
    private int contentviewid;
    private int width;
    private int height;
    private boolean fouse;
    private boolean outsidecancel;
    private int animstyle;
    public Builder(Context context){
        mContext = context;
    }
    public Builder setContentView(int contentviewid){
        this.contentviewid = contentviewid;
        return this;
    }
    public Builder setwidth(int width){
        this.width = width;
        return this;
    }
    public Builder setheight(int height){
        this.height = height;
        return this;
    }
    public Builder setFouse(boolean fouse){
        this.fouse = fouse;
        return this;
    }
    public Builder setOutSideCancel(boolean outsidecancel){
        this.outsidecancel = outsidecancel;
        return this;
    }
    public Builder setAnimationStyle(int animstyle){
        this.animstyle = animstyle;
        return this;
    }
    public Pop builder(){
        return new Pop(this);
    }
}}
然后就是调用了:
 Pop pop=new Pop.Builder(MainActivity.this)//传入参数,上下文
         .setContentView(R.layout.popppp)//布局
         .setwidth(LinearLayout.LayoutParams.WRAP_CONTENT)//宽度
         .setheight(LinearLayout.LayoutParams.WRAP_CONTENT)//高度
         .setFouse(true)//焦点
         .setOutSideCancel(true)
        .builder()
      ;
//获得View,用来查找弹窗中的控件
        View initview =   pop.initview();

        TextView text=initview.findViewById(R.id.text);
         text.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 Toast.makeText(MainActivity.this,"tanchuang",Toast.LENGTH_LONG).show();
             }
         });


    }
});
完成,一个封装就完成了,只要传入不同的布局参数,就能弹出不同的弹窗,当然,动画和颜色的也可以自己设置传入,只要在Builder中传入就可以了。
我的比较简单,不是特别容易看懂,你们可以看看这个,我的也是照着他的改的:

http://blog.csdn.net/pbm863521/article/details/74942930



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值