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>
效果图如下
;
下面这个链接写的也很不错大家可以看下: