Android Popwindow使用总结

Android Popwindow使用总结

1.基本使用方法
View view = getLayoutInflater().inflate(R.layout.activity_photo_preview, null);
......

 if (popupBigPhoto == null) {
            popupBigPhoto = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true); popupBigPhoto.setOutsideTouchable(true); popupBigPhoto.setOnDismissListener(this); } if (popupBigPhoto.isShowing()) { popupBigPhoto.dismiss(); } else { popupBigPhoto.showAtLocation(headview, Gravity.TOP, 0, 0); } 
2.属性方法

1.基本属性方法

// 设置PopupWindow的背景
 window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
// 设置PopupWindow是否能响应外部点击事件 window.setOutsideTouchable(true); // 设置PopupWindow是否能响应点击事件 window.setTouchable(true); 

2.在弹窗出现后让背景变暗,并在弹窗消失后让背景还原

window.setOnDismissListener(new PopupWindow.OnDismissListener(){
@Override
public void onDismiss() { WindowManager.LayoutParams lp=getWindow().getAttributes(); lp.alpha=1.0f; getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHND); getWindow().setAttributes(lp); } }); window.showAtLocation(activityPopup, Gravity.BOTTOM, 0, 0); WindowManager.LayoutParams lp=getWindow().getAttributes(); lp.alpha=0.3f; getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); getWindow().setAttributes(lp); 

3.添加动画
自定义一个动画

<!-- res/values/styles.xml -->
<style name="animTranslate"> <item name="android:windowEnterAnimation">@anim/translate_in</item> <item name="android:windowExitAnimation">@anim/translate_out</item> </style> 

添加动画

window.setAnimationStyle(R.style.animTranslate); 
3.位置设置
  1. 相对于父布局的位置
public void showAtLocation(View parent, int gravity, int x, int y) 第二个参数gravity指的是popupWindow在父布局中出现的大致位置。常见的有 Gravity.NO_GRAVITY,Gravity.LEFT,Gravity.RIGHT,Gravity.TOP,Gravity.BOTTOM。 第三个参数int x指的是以第二个参数gravity指点的位置为原点,popupWindow相对于原点X轴上的位置。x为正popupWindow向右移动,x为负popupWindow向左移动。 第四个参数int y同X差不多,指的是y轴上的位置。y为正popupWindow向上,y为负popupWindow向下。 
  1. 相对于某个控件的位置
public void showAsDropDown(View anchor) public void showAsDropDown(View anchor, int xoff, int yoff) public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) 前两个方法不指定gravity 则popupWindow出现在anchor的正下方。 第一个参数anchor指的是你的popupWindow相对于的这个控件。 第二个参数xoff指的是popupWindow相对于原点X轴上的位置。x为正popupWindow向右移动,x为负popupWindow向左移动。 第三个参数yoff指的是popupWindow相对于原点y轴上的位置。y为正popupWindow向下,y为负popupWindow向上。 
4.popwindow被软键盘遮挡实现方式
private void showPop(View view) { if (popWindow != null && imms != null) { imms.toggleSoftInput(0, InputMethodManager.SHOW_FORCED); } if (popWindow == null) { imms = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE); View layout = LayoutInflater.from(this).inflate(R.layout.live_qa_saysth, null); ...... popWindow = new PopupWindow(layout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true); popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); imms.toggleSoftInput(0, InputMethodManager.SHOW_FORCED); popWindow.setBackgroundDrawable(new ColorDrawable(0xb0000000)); popWindow.setOutsideTouchable(true); } if (!popWindow.isShowing()) { popWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0); } else { popWindow.dismiss(); } } 
注意点
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.setOutsideTouchable(true); 

只有同时设置PopupWindow的背景和可以响应外部点击事件,它才能“真正”响应外部点击事件。也就是说,当你点击PopupWindow的外部或者按下“Back”键时,PopupWindow才会消失。

特殊情况处理:

1.在popwindow中嵌套viewpager时候,关于定位问题:首先保证viewpager类是同一个,就是没有新new一个类。然后在show的时候记得setCurrentItem()一下就好了。

参考资料

Android PopupWindow使用方法小结
Android中文API——PopupWindow

转载于:https://www.cnblogs.com/it-tsz/p/11182947.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值