import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
代码
private ImageView sim;
private PopupWindow popupWindow;
private int from;
sim.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
from = Location.BOTTOM.ordinal();
initPopupWindow();
}
});
class popupDismissListener implements PopupWindow.OnDismissListener {
@Override
public void onDismiss() {
backgroundAlpha(1f);
}
}
protected void initPopupWindow() {
View popupWindowView = getLayoutInflater().inflate(R.layout.pop, null);
//内容,高度,宽度
popupWindow = new PopupWindow(popupWindowView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);
//菜单背景色
ColorDrawable dw = new ColorDrawable(0xffffffff);
popupWindow.setBackgroundDrawable(dw);
//显示位置
popupWindow.showAtLocation(getLayoutInflater().inflate(R.layout.activity_main, null), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
//设置背景半透明
backgroundAlpha(0.5f);
//关闭事件
popupWindow.setOnDismissListener(new popupDismissListener());
popupWindowView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
popupWindow = null;
}
// 这里如果返回true的话,touch事件将被拦截
// 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss
return false;
}
});
Button open = (Button) popupWindowView.findViewById(R.id.open);
Button save = (Button) popupWindowView.findViewById(R.id.save);
Button close = (Button) popupWindowView.findViewById(R.id.close);
//调用相机
open.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 这个出捕获图片的常量值
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 设置图片输出位置; 输出到制定的uri路径上;
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path)));
// 设置请求码
startActivityForResult(intent, 100);
popupWindow.dismiss();
}
});
//调用相册
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 101);
popupWindow.dismiss();
}
});
close.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
}
public void backgroundAlpha(float bgAlpha) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = bgAlpha; //0.0-1.0
getWindow().setAttributes(lp);
}
public enum Location {
BOTTOM;
}
pupwindow布局
<?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">
<Button
android:id="@+id/open"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="相机" />
<Button
android:id="@+id/save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="图库" />
<Button
android:id="@+id/close"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="关闭" />
</LinearLayout>