您的位置:首页 > 运维架构

PopupWindow的使用,及其注意事项

2016-08-15 09:10 288 查看
1、PopupWindow与AlertDialog的区别

主要是对话框不能指定位置,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)了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: