在Activity中
mPopwindow = new CameraPopwindow(getActivity(), itemsOnClick);
mPopwindow.setFocusable(true); // 获取焦点
mPopwindow.setOutsideTouchable(true);
mPopwindow.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
复制代码
重写[Pop]
public class CameraPopwindow extends PopupWindow {
private View mView;
public CameraPopwindow(Activity context, View.OnClickListener itemsOnClick) {
super(context);
initView(context, itemsOnClick);
}
private void initView(final Activity context, View.OnClickListener itemsOnClick) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = mInflater.inflate(R.layout.camerapopwendow, null);
TextView camera= (TextView) mView.findViewById(R.id.Personal_fragment_camera);
TextView album= (TextView) mView.findViewById(R.id.Personal_fragment_album);
TextView personal_dimess= (TextView) mView.findViewById(R.id.Personal_fragment_dimess);
personal_dimess.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//销毁弹出框
dismiss();
backgroundAlpha(context, 1f);
}
});
camera.setOnClickListener(itemsOnClick);
album.setOnClickListener(itemsOnClick);
//设置SelectPicPopupWindow的View
this.setContentView(mView);
//设置SelectPicPopupWindow弹出窗体的宽
this.setWidth(WindowManager.LayoutParams.FILL_PARENT);
//设置SelectPicPopupWindow弹出窗体的高
this.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
//设置SelectPicPopupWindow弹出窗体可点击
this.setFocusable(true);
//设置PopupWindow可触摸
this.setTouchable(true);
//设置非PopupWindow区域是否可触摸
ColorDrawable dw = new ColorDrawable(0x00000000);
//设置SelectPicPopupWindow弹出窗体的背景
this.setBackgroundDrawable(dw);
backgroundAlpha(context, 0.5f);//0.0-1.0
this.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
// TODO Auto-generated method stub
backgroundAlpha(context, 1f);
}
});
}
/**
* 设置添加屏幕的背景透明度
*
* @param bgAlpha
*/
public void backgroundAlpha(Activity context, float bgAlpha) {
WindowManager.LayoutParams lp = context.getWindow().getAttributes();
lp.alpha = bgAlpha;
context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
context.getWindow().setAttributes(lp);
}
}
//不要忘记最后写上~~~~~
//Dialog窗体泄露解决
@Override
public void onDestroy() {
super.onDestroy();
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
dialog = null;
}
}
复制代码
CameraPopwindow LinearLayout XML
<LinearLayout
android:background="@drawable/youmeng_btnimg"
android:layout_marginBottom="@dimen/default_50dp"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_marginTop="@dimen/default_10sp"
android:id="@+id/Personal_fragment_camera"
android:gravity="center"
android:text="相机"
android:layout_width="match_parent"
android:layout_height="@dimen/default_30dp" />
<TextView
android:id="@+id/Personal_fragment_album"
android:gravity="center"
android:text="相册"
android:layout_width="match_parent"
android:layout_height="@dimen/default_30dp" />
<TextView
android:id="@+id/Personal_fragment_dimess"
android:gravity="center"
android:text="关闭"
android:layout_width="match_parent"
android:layout_height="@dimen/default_30dp" />
</LinearLayout>
复制代码