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>


你可能感兴趣的:(android,开发,工具,PopupWindow)