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

最新文章

  1. (转)modelsim10.0C编译ISE14.7的xilinx库(xilinx ip核)
  2. Windows 内存架构
  3. 网络编程Socket UDP
  4. poj2993 poj2669
  5. editplus使用:非法字符: \65279
  6. Spring学习(10)--- @Qualifier注解
  7. Hadoop百度百科
  8. 基于apache httpclient 调用Face++ API
  9. 论Photoshop的正确安装姿势
  10. 异步启动solidworks
  11. 【剑指offer】两个链表的第一个公共结点
  12. [转] LoadRunner 获取接口请求响应信息
  13. redis集群报错:(error) MOVED 11469 192.168.163.249:7002
  14. python_ssh连接
  15. Postfix邮件
  16. 自定义WordPress文件上传路径
  17. paramiko 简单的使用
  18. (转)MySQL高可用解决方案
  19. mysql触发器小实验
  20. java中用正则表达式判断中文字符串中是否含有英文或者数字

热门文章

  1. java利用MultipartRequest的getFileName方法不能得到原文件名问题
  2. Docker pull下载出现 error pulling image configuration:
  3. MySQL无法启动问题解决Warning: World-writable config file ‘/etc/my.cnf’ is ignored
  4. FreeRTOS任务基础概念
  5. jQuery效果函数
  6. 【javascript】h5页面禁止返回上一页
  7. hi 北京
  8. 第82题:删除排序链表中的重复元素II
  9. CodeForces 840A - Leha and Function | Codeforces Round #429 (Div. 1)
  10. java.sql.SQLException: 不支持的字符集 (在类路径中添加 orai18n.jar): ZHS16GBK