popupwindow在開發過程中經常都會用到,他的效果就像一個布蓋在了界面上,讓界面失去焦點,下面我們來說下popupwindow的實現:
popupwindow顯示的界面其實和Activity顯示的基本上一樣,也是通過
LayoutInflater inflater = (LayoutInflater)context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View mMenuView = inflater.inflate(R.layout.popwindow_camerapic, null);調用layout佈局來顯示界面想要顯示的內容;
下面我們就用一個調用系統的相機和相冊,來說下popupwindow的應用,先上代碼:
public class SelectPicPopupWindow extends PopupWindow implements View.OnClickListener { private View btn_take_photo, btn_pick_photo, btn_cancel; private View mMenuView; private OnClickListener mCallback; public SelectPicPopupWindow(Context context, OnClickListener listener) { super(context); LayoutInflater inflater = (LayoutInflater)context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); mMenuView = inflater.inflate(R.layout.popwindow_camerapic, null); mCallback = listener; btn_take_photo = mMenuView.findViewById(R.id.btn_takepic); btn_pick_photo = mMenuView.findViewById(R.id.btn_picalbum); btn_cancel = mMenuView.findViewById(R.id.btn_cancelpic); // 取消按鈕 btn_cancel.setOnClickListener(new OnClickListener() { public void onClick(View v) { // 銷燬彈出框 dismiss(); } }); // 設置按鈕監聽 btn_pick_photo.setOnClickListener(this); btn_take_photo.setOnClickListener(this); // 設置SelectPicPopupWindow的View // 設置SelectPicPopupWindow彈出窗體的寬 setWidth(ViewGroup.LayoutParams.MATCH_PARENT); // 設置SelectPicPopupWindow彈出窗體的高 setHeight(ViewGroup.LayoutParams.MATCH_PARENT); // 設置SelectPicPopupWindow彈出窗體可點擊 setFocusable(true); // 設置SelectPicPopupWindow彈出窗體動畫效果 setAnimationStyle(R.style.PopupAnimation); // 實例化一個ColorDrawable顏色爲半透明 // 設置SelectPicPopupWindow彈出窗體的背景 setBackgroundDrawable(new ColorDrawable(0x50000000)); // mMenuView添加OnTouchListener監聽判斷獲取觸屏位置如果在選擇框外面則銷燬彈出框 setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); mMenuView.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP && event.getY() < mMenuView.findViewById(R.id.rl_popwindow_camerapic) .getTop()) { dismiss(); } return true; } }); setContentView(mMenuView); } @Override public void onClick(View v) { dismiss(); if (mCallback != null) { mCallback.onClick(v); } } }activity要點代碼
public class CreateActivity extends Activity implements View.OnClickListener{ 。。。。。。 。。。。。。 ivHeadImage = (ShapeImageView)view.findViewById(R.id.civ_create_card_head);
llCreateCard = (LinearLayout)view.findViewById(R.id.ll_create_card);ivHeadImage.setOnClickListener(this);}
調用popupwindow:
@Override public void showDialogChangeHeadImage() { SelectPicPopupWindow cSelectPicPopupWindow = new SelectPicPopupWindow(getActivity(), this); // 顯示窗口 cSelectPicPopupWindow.showAtLocation(llCreateCard, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); }
動態效果
<!--popwindow stype--> <style name="PopupAnimation" parent="android:Animation"> <item name="android:windowEnterAnimation">@anim/push_bottom_in</item> <item name="android:windowExitAnimation">@anim/push_bottom_out</item> </style>
調用popupwindow的按鈕的監聽和照相和相冊的監聽回調
@Override public void onClick(View v) { switch (v.getId()) { case R.id.civ_create_card_head: showDialogChangeHeadImage(); break; case R.id.btn_takepic: GetPhotoWay.getInstance().takePhoto(getActivity(), true, 0, CreateCardConfigs.CREATE_CARD_FOR_RESULT_CAMERA); break; case R.id.btn_picalbum: GetPhotoWay.getInstance().choosePhoto(getActivity(), CreateCardConfigs.CREATE_CARD_FOR_RESULT_PICTURE); break; case R.id.iv_back: if (!TextUtils.isEmpty(enterType) && enterType.equals(CreateCardConfigs.createCardNoTitleEnter)) { mListener.openShowCard(); } else { getActivity().finish(); } break; default: break; } }
popupwindow的佈局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/transparent" > <RelativeLayout android:focusable="true" android:focusableInTouchMode="true" android:id="@+id/rl_popwindow_camerapic" android:layout_width="match_parent" android:layout_height="205dip" android:layout_alignParentBottom="true" android:paddingBottom="17dip" android:paddingLeft="9dip" android:paddingRight="9dip" > <!-- 取消按鈕 --> <Button android:id="@+id/btn_cancelpic" android:layout_width="match_parent" android:layout_height="51dip" android:layout_alignParentBottom="true" android:background="@drawable/btn_pop_only_center" android:gravity="center" android:text="@string/cancel" android:textColor="@color/guide_blue" android:textSize="20dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@id/btn_cancelpic" android:layout_marginBottom="9dip" android:orientation="vertical" > <!-- 拍照按鈕 --> <Button android:id="@+id/btn_takepic" android:layout_width="match_parent" android:layout_height="50dip" android:background="@drawable/btn_pop_choose_pic_up" android:gravity="center" android:text="@string/photograph" android:textColor="@color/guide_blue" android:textSize="20dp" /> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/guide_divider" /> <!-- 手機相冊 --> <Button android:id="@+id/btn_picalbum" android:layout_width="match_parent" android:layout_height="50dip" android:background="@drawable/btn_pop_choose_pic_down" android:gravity="center" android:text="@string/choose_from_album" android:textColor="@color/guide_blue" android:textSize="20dp" /> </LinearLayout> </RelativeLayout> </RelativeLayout>