android-PopupWindow弹出窗口 - 随心

Android的对话框有两种:PopupWindow和AlertDialog。它们的不同点在于:

  • AlertDialog的位置固定,而PopupWindow的位置可以随意
  • AlertDialog是非阻塞线程的,而PopupWindow是阻塞线程的

PopupWindow的位置按照有无偏移分,可以分为偏移和无偏移两种;按照参照物的不同,可以分为相对于某个控件(Anchor锚)和相对于父控件。具体如下

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

不解释之际上代码

LayoutInflater inflater = (LayoutInflater) DeliveryActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.popup_sms, null);

LinearLayout popup_contact = (LinearLayout)view.findViewById(R.id.popup_contact);
LinearLayout popup_input = (LinearLayout)view.findViewById(R.id.popup_input);
popup_contact.setOnClickListener(DeliveryActivity.this);
popup_input.setOnClickListener(DeliveryActivity.this);

selectSMSPopup = new PopupWindow(view);
selectSMSPopup.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
selectSMSPopup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
// 必须设置,否则获得焦点后页面上其他地方点击无响应
selectSMSPopup.setBackgroundDrawable(new BitmapDrawable());
// 设置焦点,必须设置,否则listView无法响应
selectSMSPopup.setFocusable(true);
// 设置点击其他地方 popupWindow消失
selectSMSPopup.setOutsideTouchable(true);
// 显示在某个位置
selectSMSPopup.showAsDropDown(LinearLayout);
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值