前几天用到了PopupWindow,在PopupWindow上面放了个ListView,在使用过程中遇到了以下问题:

一、PopupWindow上面的ListView无法点击;

二、PopupWindow上面ListView的高度调整;

三、PopupWindow无法用Back键取消。

下面来具体介绍一下PopupWindow的使用:

首先布局PopupWindow:1个ListView

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:baselineAligned="false"    android:orientation="vertical" >    <LinearLayout        android:id="@+id/popListLayout"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical" >        <ListView            android:id="@+id/father_lv"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:background="#CCCCCC" >        </ListView>    </LinearLayout>    <LinearLayout        android:id="@+id/popfooterLayout"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical"        android:background="#99000000" >    </LinearLayout></LinearLayout>

然后初始化PopupWindow

View mypopupView = getLayoutInflater().inflate(R.layout.popup_item,null, false);list = new ArrayList<String>();for(int i=0;i<10;i++){list.add("item"+i);}fatherLV = (ListView) mypopupView.findViewById(R.id.father_lv);fatherLV.setAdapter(new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1,list));fatherLV.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {// TODO Auto-generated method stubToast.makeText(MainActivity.this, "position"+arg2, 1).show();}});;popView = new PopupWindow(mypopupView, -2, -2);

初始化完毕就可以在需要的地方调用了,不过这样直接调用的结果是ListView不能点击,应该在初始化的时候加上如下代码:

// 加上这个属性,PopupWindow的ListView的Item才可以点击popView.setFocusable(true);

调用及取消

if (popView != null&&popView.isShowing()) {popView.dismiss();return;} else {initPop();popView.showAsDropDown(v, 0, 0);}

使用如上方法调用PopupWindow,再次点击会取消,但是如上设置后,发现还是不能取消,需要给PopupWindow添加OnTouchListener,这样便可以取消PopupWindow,还可以在点击PopupWindow的空白区域的时候取消PopupWindow

mypopupView.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {if (popView != null && popView.isShowing()) {popView.dismiss();popView = null;}return false;}});

ListView高度问题:

先看效果:

当我们需要下面始终留有一部分空白的时候,就需要调整ListView的高度,不在xml文件里面设置具体高度的情况下,我们可以通过如下Java代码设置,取屏幕一半的高度作为ListView的高度

DisplayMetrics metric = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(metric);int height = metric.heightPixels;   // 屏幕高度(像素)LinearLayout.LayoutParams popLp = new LayoutParams(-1, height/2);fatherLV.setLayoutParams(popLp);

Back键取消PopupWindow:

这个问题很让人蛋疼,虽然解决了,但是还是布吉岛为什么这样做就可以了,一句话解决问题:别看是设置背景,不会对你的背景产生任何的影响哦。

popView.setBackgroundDrawable(new BitmapDrawable());

如上就是我使用popupWindow的时候遇到的几个问题,希望能帮到大家。


更多相关文章

  1. Android(安卓)实现通知栏显示多个notification
  2. 一段android跳转到web界面的代码
  3. Android(安卓)Studio 单元测试之简单版
  4. Android(安卓)studio安装与配置Butter knife过程
  5. textview点击后selector的pressed无效果
  6. Android使用ListView构造复杂页面,并响应点击事件,通过intent跳转
  7. Android:入门、开发环境搭建、第一个Android程序、源码下载
  8. EClipse 项目导入 Android(安卓)Studio 导致的乱码问题
  9. 【IOS】实现IOS版的抽屉效果(点击,拖拽滑动)

随机推荐

  1. Android(安卓)Studio 使用lint工具进行代
  2. Mac OS 上配置Android(安卓)studio的真机
  3. 系统定义弹窗以及自定义弹窗
  4. 用Android自带的DrawerLayout和ActionBar
  5. Android(安卓)自定义Notification字体颜
  6. Android(安卓)属性动画 实现view翻转 旋
  7. 密码输入框
  8. Android(安卓)Recovery的汉化 显示中文
  9. BroadcastReceive介绍
  10. Android(安卓)和 前端的类似点(持续更新)