Android PopupWindow实战仿QQ底部弹出

来源:互联网 发布:me域名不能注册 编辑:程序博客网 时间:2024/04/30 20:21

Android提供两种对话框形式,一种是popupwindow,一种是dialog,先为大家说明一下两者的区别。

位置:AlertDialog的位置固定,而PopupWindow的位置可以随意。

showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移

showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移

showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移

线程:AlertDialog是非阻塞线程的,而PopupWindow是阻塞线程的。

第一个案例


讲解一下重要的代码部分,稍后我会讲源码发出去供大家下载。

简单的案例,大家先看一下代码

public class MyPopupWindow extends PopupWindow {private Button btn_take_photo, btn_pick_photo, btn_cancel;private View mMenuView;public MyPopupWindow(Activity context, OnClickListener itemsOnClick) {super(context);LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);mMenuView = inflater.inflate(R.layout.alert_dialog, null);btn_take_photo = (Button) mMenuView.findViewById(R.id.btn_take_photo);btn_pick_photo = (Button) mMenuView.findViewById(R.id.btn_pick_photo);btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel);//取消按钮btn_cancel.setOnClickListener(new OnClickListener() {public void onClick(View v) {//销毁弹出框dismiss();}});//设置按钮监听btn_pick_photo.setOnClickListener(itemsOnClick);btn_take_photo.setOnClickListener(itemsOnClick);this.setContentView(mMenuView);this.setWidth(LayoutParams.FILL_PARENT);this.setHeight(LayoutParams.WRAP_CONTENT);this.setFocusable(true);this.setTouchable(true);this.setAnimationStyle(R.style.PopupAnimation);ColorDrawable dw = new ColorDrawable(Color.TRANSPARENT);this.setBackgroundDrawable(dw);//mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框mMenuView.setOnTouchListener(new OnTouchListener() {public boolean onTouch(View v, MotionEvent event) {int height = mMenuView.findViewById(R.id.pop_layout).getTop();int y = (int) event.getY();if (event.getAction() == MotionEvent.ACTION_UP) {if (y < height) {dismiss();}}return true;}});}}
看一下MainActivity的实现

public class MainActivity extends Activity {//自定义的弹出框类MyPopupWindow menuWindow;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);final TextView tv = (TextView) this.findViewById(R.id.text);//把文字控件添加监听,点击弹出自定义窗口tv.setOnClickListener(new OnClickListener() {public void onClick(View v) {menuWindow = new MyPopupWindow(MainActivity.this,itemsOnClick);//显示窗口menuWindow.showAtLocation(MainActivity.this.findViewById(R.id.main), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置}});}//为弹出窗口实现监听类private OnClickListener itemsOnClick = new OnClickListener() {public void onClick(View v) {menuWindow.dismiss();switch (v.getId()) {case R.id.btn_take_photo:break;case R.id.btn_pick_photo:break;default:break;}}};}

下载Demo请猛戳

第二个案例

①.如上图所示在构造PopupWindow的实例时,制定它的宽度是button的宽度,如下代码

PopupWindow popu = new PopupWindow(mPopup, btn.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
②.点击外部时,当前PopupWindow就消失,如下代码

popu.setOutsideTouchable(true);
③.设置PopupWindow可以获取焦点

popu.setFocusable(true);
④.调用显示位置的showAtLocation方法

int[] location = new int[2];btn.getLocationOnScreen(location);popu.showAtLocation(btn, Gravity.NO_GRAVITY, location[0], location[1] + btn.getHeight());
下载Demo请猛戳

第三个案例


关键是在ListView的Adapter中对popupwindow的处理

在构建Adapter实例的时候,初始化popupwindow,并显示

/**  * 初始化popWindow * */private void initPopWindow() {View popView = inflater.inflate(R.layout.listview_pop, null);popupWindow = new PopupWindow(popView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);popupWindow.setBackgroundDrawable(new ColorDrawable(0));//设置popwindow出现和消失动画popupWindow.setAnimationStyle(R.style.PopMenuAnimation);btn_pop_close = (ImageView) popView.findViewById(R.id.btn_pop_close);}
/**  * 显示popWindow * */public void showPop(View parent, int x, int y, int postion) {//设置popwindow显示位置popupWindow.showAtLocation(parent, Gravity.AXIS_X_SHIFT, x, y);//获取popwindow焦点popupWindow.setFocusable(true);//设置popwindow如果点击外面区域,便关闭。popupWindow.setOutsideTouchable(true);popupWindow.update();if (popupWindow.isShowing()) {popupWindow.dismiss();}btn_pop_close.setOnClickListener(new OnClickListener() {public void onClick(View paramView) {popupWindow.dismiss();}});}
下载Demo请猛戳




1 0