popupWindow 的使用

废话不说,直接上代码




 private void showPopupWindow(View view) {
        //一个自定义 布局,作为显示的内容
        View contentView = LayoutInflater.from(this).inflate(R.layout.activity_myinfo_popupwindow, null);
        //设置按钮的点击事件
        Button bt1 = (Button) contentView.findViewById(R.id.myinfo_popup_button1);
        Button bt2 = (Button) contentView.findViewById(R.id.myinfo_popup_button2);
        //创建出PopupWindow
        final PopupWindow popupWindow = new PopupWindow(contentView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.toast(MyInfoActivity.this, "点击了bt1");
                popupWindow.dismiss();
            }
        });
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.toast(MyInfoActivity.this, "点击了bt2");
                popupWindow.dismiss();
            }
        });
        // 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug

        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.mipmap.book_page_bg2));
        //设置宽度
        popupWindow.setWidth(view.getWidth() / 4 * 3);
        //设置显示位置
        popupWindow.showAtLocation(contentView, Gravity.CENTER, 0, -100);
        //可点击
        popupWindow.setTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
       
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    popupWindow.dismiss();
                    return true;
                }
                //如果返回true,touch事件将被拦截
                //拦截后,popupWindow的OnTouchEvennt不被调用,这样点击外部区域无法dismiss
                return false;

            }
        });


        //将popup展示出来
        popupWindow.showAsDropDown(view);

    }

    private void initAlert() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);


    }



布局



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@mipmap/book_page_bg1">


    <Button
        android:id="@+id/myinfo_popup_button1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#fffffff0"
        android:onClick="onclick"
        android:text="从相册中选取"
        android:textSize="18sp" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp" 
        
        android:background="#eeeeeeee"/>

    <Button
        android:id="@+id/myinfo_popup_button2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#fffffff0"
        android:onClick="onclick"
        android:text="拍照"
        android:textSize="18sp" />

</LinearLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值