PopupWindow的使用,及其注意事项
2016-08-15 09:10
288 查看
1、PopupWindow与AlertDialog的区别
主要是对话框不能指定位置,popupwindow可以.
2.使用
(1)布局文件
主要是对话框不能指定位置,popupwindow可以.
2.使用
(1)布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="@drawable/menu" android:paddingLeft="15dp" android:paddingRight="15dp" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_marginTop="30dp" android:layout_marginBottom="12dp" style="@style/TextView.add" android:id="@+id/tv_toSmall" android:background="@drawable/class_list_bg" android:layout_gravity="center" android:text="@string/baby_pop1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <View android:background="#ECF0F9" android:layout_width="match_parent" android:layout_height="1dp"/> <TextView style="@style/TextView.add" android:layout_marginTop="12dp" android:layout_marginBottom="12dp" android:id="@+id/tv_check_least" android:background="@drawable/class_list_bg" android:layout_gravity="center" android:text="@string/baby_pop2" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <View android:background="#ECF0F9" android:layout_width="match_parent" android:layout_height="1dp"/> <TextView style="@style/TextView.add" android:layout_marginTop="12dp" android:layout_marginBottom="12dp" android:background="@drawable/class_list_bg" android:id="@+id/tv_choice_online" android:layout_gravity="center" android:text="@string/baby_pop3" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
(2)使用
/** * 显示下拉框 */ private void showPopupWindow() { View contentView = LayoutInflater.from(this).inflate(R.layout.baby_pop, null); mPopWindow = new PopupWindow(contentView);//创建,PopupWindow是没有默认布局的
//设置宽高, mPopWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); mPopWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
//里边控件的点击事件 TextView tvThum = (TextView) contentView.findViewById(R.id.tv_toSmall); TextView tvLeast = (TextView) contentView.findViewById(R.id.tv_check_least); TextView tvChoicOnline = (TextView) contentView.findViewById(R.id.tv_choice_online); tvThum.setOnClickListener(this); tvLeast.setOnClickListener(this); tvChoicOnline.setOnClickListener(this); mPopWindow.setBackgroundDrawable(new BitmapDrawable());//必须设置,否则点击四周pop不会消失 mPopWindow.setOutsideTouchable(true);//设置四周点击,pop消失 mPopWindow.showAsDropDown(imgRight);//设置显示位置 }3.注意
构建在某个Activity之上的对话框、PopupWindow也有相应的WindowManager窗体管理器。因为对话框、PopupWindown不能脱离Activity而单独存在着,
所以当某个Dialog或者某个PopupWindow正在显示的时候我们去finish()了承载该Dialog(或PopupWindow)的Activity时,就会抛Window Leaked异常了,因为这个Dialog(或PopupWindow)的WindowManager已经没有谁可以附属了,所以它的窗体管理器已经泄漏了。
解决方法: 关闭(finish)某个Activity前,要确保附属在上面的Dialog或PopupWindow已经关闭(dismiss)了。
相关文章推荐
- 使用libgdx及其中的box2d 2.1的注意事项
- popupWindow的使用和注意事项
- web开发之图解请求转发与重定向及其使用注意事项
- 【IO】ObjectInputStream的使用方法及其注意事项
- malloc()动态分配内存的使用及其注意事项
- C语言位域使用及其注意事项
- SWFUpload的使用及其注意事项
- Snackbar使用及其注意事项(转)
- 黑马程序员——集合框架的使用及其注意事项
- 环形光的发光特征及其使用注意事项
- 常用sql命令@oracle数据类型概括@权限、角色、用户的创建于使用@伪列及其注意事项
- 使用OpenSSL工具制作X.509证书的方法及其注意事项总结
- 使用OpenSSL工具制作X.509证书的方法及其注意事项总结
- popupwindow的使用及注意事项
- 请跟我来(完)--使用Ext及其llano的注意事项
- sqlite3的绑定函数族使用,及其注意事项
- android:PopupWindow的使用场景和注意事项
- android:PopupWindow的使用场景和注意事项
- (4.1.43.3)design support library:Snackbar使用及其注意事项
- angularjs $broadcast 和 $on 的使用及其注意事项