PopupWindow怎么用?

来源:互联网 发布:廖雪峰python教程pdf 编辑:程序博客网 时间:2024/06/10 17:53
一个Activity里有两个Fragment,fragment里面放的是一个Listview,
我点一个条目中的button时,在屏幕的下方弹出来一个popupWindow,要弹出来popupWindow,要设置
pop.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);
这个时候的parentView要传谁?
我现在传的是fragment所在的activity的布局,这个是报错的,代码:

new popupWindow和给popupWindow填充布局的代码:
public static PopupWindow createPopupWindow(Context context, final popupWindowCallback callback)
    {
        final PopupWindow mPopupwindow = new PopupWindow(context);
//         View view = View.inflate(context, R.layout.add_goods_popup,null);
        View view = LayoutInflater.from(context).inflate(R.layout.add_goods_popup, null);;
               
        root_popup = (RelativeLayout) view.findViewById(R.id.root_popup);
        mPopupwindow.setWidth(LayoutParams.MATCH_PARENT);
        mPopupwindow.setHeight(LayoutParams.WRAP_CONTENT);
        mPopupwindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupwindow.setFocusable(true);
        mPopupwindow.setOutsideTouchable(true);
        mPopupwindow.setContentView(view);
        
        ImageButton ib_pop_close = (ImageButton) view.findViewById(R.id.ib_pop_close);
        // 整个线性布局
        root_popup.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mPopupwindow.dismiss();
//                 root_popup.clearAnimation();
            }
        });
        return mPopupwindow;
        
    }

使用popupWindow时的代码:
pop =Utils.createPopupWindow(context.getApplicationContext(), new popupWindowCallback() {

       @Override
       public void cancelClickListener() {

       }      
      pop.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);
}

因为是在listview的item中点击button时弹出来popupWindow的,所以代码也就放在adapter类里面了,只要一执行下面代码就
崩溃了。
pop.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);
0 0
原创粉丝点击