首先在上节中是使用dialog 实现的,(点击连接),现在我就讲些popupwindow 的实现,这个相对dialog比较简单,因为不用自定义dialog.
实现代码很简单如下:
代码片段:
public void showPopupWindow(int x, int y) {
layout = (LinearLayout) LayoutInflater.from(MainActivity.this).inflate(
R.layout.dialog, null);
listView = (ListView) layout.findViewById(R.id.lv_dialog);
listView.setAdapter(new ArrayAdapter<String>(MainActivity.this,
R.layout.text, R.id.tv_text, title));
popupWindow = new PopupWindow(MainActivity.this);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow
.setWidth(getWindowManager().getDefaultDisplay().getWidth() / 2);
popupWindow.setHeight(300);
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setContentView(layout);
// showAsDropDown会把里面的view作为参照物,所以要那满屏幕parent
// popupWindow.showAsDropDown(findViewById(R.id.tv_title), x, 10);
popupWindow.showAtLocation(findViewById(R.id.main), Gravity.LEFT
| Gravity.TOP, x, y);//需要指定Gravity,默认情况是center.
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
button.setText(title[arg2]);
popupWindow.dismiss();
popupWindow = null;
}
});
}
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
button.getTop();
int y = button.getBottom() * 3 / 2;
int x = getWindowManager().getDefaultDisplay().getWidth() / 4;
showPopupWindow(x, y);
}
});
样子我就不贴了,和前面一章dialog显示的一样.
在这里遇到个小问题:int y = button.getBottom() * 3 / 2;这里获取的y坐标应该是button.getBottom();可是这样写popupwindow就显示位置不对了,在button中间,不知道为什么。
如果知道为什么了通知我一下,共同学习嘛,先谢谢了。
/*****************************纠正错误**********************************************************/
首先上面int y = button.getBottom() * 3 / 2;
这个是错误的,只是偶尔符合了,实际上我少加了18像素.因为少加了一个状态栏的高度
正确写法:
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
state_heght = frame.top;// 状态栏的高度
int y = button.getBottom()+state_heght ;
int x = getWindowManager().getDefaultDisplay().getWidth()/4;
showPopupWindow(x, y);
我们看一张图:
取消状态栏 没有加状态栏高度 加状态栏高度
其实popupwindow用起来比Dialog 方便,因为dialog一切都需要你自己来实现,而popupwindow帮我们实现了很多,我认为在使用的时候关键我们要看清参数:
popupWindow.showAtLocation(parent, gravity, x, y);
parent这个是一个父view试图,
gravity 这个默认是居中,我们要指定显示方式:Gravity.LEFT
| Gravity.TOP
最后两个参数就是显示的坐标位置,这样就可以根据需求自己定位显示位置了.
好了,讲的不是很详细,不过之前那个问题解决了,以后在深究吧。
源码下载
分享到:
相关推荐
popupwindow 模拟新浪、腾讯title弹框效果 popupwindow 模拟新浪、腾讯title弹框效果
android popupwindow 仿新浪、腾讯title弹框效果
android PopupWindow的使用(带动画效果)源码
android popupwindow 底部灰色背景
Android PopupWindow实现右侧、左侧和底部弹出菜单 效果图:http://blog.csdn.net/jianfengwen/article/details/48629217
android自定义PopupWindow,实现类似微信弹框类别选项。
Android PopupWindow与Activity之间的数据传递
Android PopupWindow泡泡效果(有listview)
Android从屏幕底部滑动弹出PopupWindow,有动画效果,类似于sharesdk的分享页面滑动弹出效果。
Android PopupWindow使用示例,更多信息请访问http://blog.csdn.net/qingdujun/article/details/42609549
Android用PopupWindow实现新浪微博的分组信息的源代码
Android PopupWindow显示在控件上方或者下方,完整的例子下载分享给有需要的人;内部填充数据用的自定义LinearLayout,可以动态添加标题,在Activity中处理回调方法.项目中摘取,有些冗余代码,亲要自己简单处理下哦...
安卓中的弹窗的Demo,有几种不同类型的弹窗PopupWindow
Android popupWindow 仿微信弹出效果我的博客http://blog.csdn.net/xiaoyuan511
Android PopupWindow显示位置常用效果 我的博客http://blog.csdn.net/xiaoyuan511/article/details/38711323
Android popupwindow 实例及使用
在android 开发中,让popupwindow 弹出在点击按钮的上方中间; 说明文档见我的博客
经过多番测试实践,实现了popupwindow 弹出在指定控件的下方。代码上有注释,有需要注意的地方。popupwindow 有自已的布局,里面控件的监听实现都有。
在PopupWindow上实现图片的缩放
android 封装一个通用PopupWindow,避免重复造轮子,简化代码,方便管理。