PopupWindow的使用

来源:互联网 发布:安卓手机刷windows系统 编辑:程序博客网 时间:2024/06/05 03:44

在安卓中使用PopupWindow类 创建弹出效果对话框

使用示例

    View view = View.inflate(this, R.layout.popup_returns_img, null);    popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,      ViewGroup.LayoutParams.WRAP_CONTENT, true);    popupWindow.setTouchable(true);    popupWindow.showAtLocation(parentView, Gravity.CENTER, 0, 0);       // 设置窗口的位置//        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.popup_background));        RelativeLayout rl1 = (RelativeLayout) view.findViewById(R.id.rl_camera);        RelativeLayout rl2 = (RelativeLayout) view.findViewById(R.id.rl_gallary);        RelativeLayout grayBackground = (RelativeLayout) view.findViewById(R.id.rl_gray_backgroud);        // 拦截自身触摸事件        LinearLayout window = (LinearLayout) view.findViewById(R.id.ll_window);        window.setOnTouchListener(new View.OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                return true;            }        });        // 点击后退按钮,销毁PopupWindow对象        grayBackground.setFocusable(true);        grayBackground.setFocusableInTouchMode(true);        grayBackground.setOnKeyListener(new View.OnKeyListener() {            @Override            public boolean onKey(View v, int keyCode, KeyEvent event) {                if(event.getAction() == KeyEvent.ACTION_DOWN && keyCode == event.getKeyCode()){                    popupWindow.dismiss();                }                return false;            }        });//        AlertDialog.Builder builder = new AlertDialog.Builder(this);//        View view = View.inflate(this, R.layout.popup_returns_img, null);////        final RadioGroup group = (RadioGroup) view.findViewById(R.id.rgrp_picker);////        builder.setView(view);//        dialog = builder.create();////        RelativeLayout rl1 = (RelativeLayout) view.findViewById(R.id.rl_camera);//        RelativeLayout rl2 = (RelativeLayout) view.findViewById(R.id.rl_gallary);        View.OnClickListener listener = new View.OnClickListener() {            @Override            public void onClick(View view) {                Intent intent;                switch (view.getId()) {                    case R.id.rl_camera:                        // 使用相机拍照                        imageDir = "temp.jpg";                        intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                        intent.putExtra(MediaStore.EXTRA_OUTPUT,                                Uri.fromFile(new File(Environment.getExternalStorageDirectory(), imageDir)));                        startActivityForResult(intent, IMAGE_CAMERA);                        break;                    case R.id.rl_gallary:                        // 从图库中选择图片//                        intent = new Intent(Intent.ACTION_GET_CONTENT);//                        intent.setType(IMAGE_UNSPECIFIED);//                        intent.setPackage("com.google.android.gallery3d");  // 指明包名,直接打开系统自带的图库程序//                        Intent wrapperIntent = Intent.createChooser(intent, null);//                        startActivityForResult(wrapperIntent, IMAGE_GALLERY);                        // 兼容Android 4.4 KITKAT                        intent = new Intent();                        intent.addCategory(Intent.CATEGORY_OPENABLE);                        intent.setType("image/jpeg");                        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {                            intent.setAction(Intent.ACTION_OPEN_DOCUMENT);                            // TODO 在IUNI4.4上 打开图库会挂掉//                            intent.addCategory(Intent.CATEGORY_DEFAULT);                            startActivityForResult(intent, IMAGE_GALLERY_KITKAT);                        } else {                            intent.setAction(Intent.ACTION_GET_CONTENT);                            startActivityForResult(intent, IMAGE_GALLERY);                        }                        break;                    case R.id.rl_gray_backgroud:                        popupWindow.dismiss();                        break;                }            }        };        rl1.setOnClickListener(listener);        rl2.setOnClickListener(listener);        grayBackground.setOnClickListener(listener);        popupWindow.showAsDropDown(view);
0 0
原创粉丝点击