PopupWindow的使用,及其注意事项

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)了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值