PopupWindow
在安卓中使用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);