在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>
複製程式碼