在使用PopupWindow的时候,有一个不好的地方就是不太好设置弹出窗体的大小。如果指定绝对大小,那么对于不同分辨率不同尺寸的手机来说,显示出来效果会不同,从而导致用户体验不佳。

为了达到PopupWindow能够自适配布局大小,可以在设置长宽时候指定:

[java] view
plain
copy

  1. popupWindow.setWidth(LayoutParams.WRAP_CONTENT);
  2. popupWindow.setHeight(LayoutParams.WRAP_CONTENT);

下面我就来具体讲解一下在PopupWindow中使用ListView的方法。

首先贴出的是main.xml

[html] view
plain
copy

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <Button android:id="@+id/button"
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:text="弹出popupWindow" />
  9. </LinearLayout>

然后贴出的是PopupWindow中显示的listview_demo.xml

[html] view
plain
copy

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <ListView android:id="@+id/listview"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent" />
  8. </LinearLayout>

再贴出的是listview显示的每一项item.xml

[html] view
plain
copy

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <TextView android:id="@+id/item"
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:textSize="18sp" />
  9. </LinearLayout>

最后贴出的是java代码PopupWindowDemoActivity.java

[java] view
plain
copy

  1. package xmu.zgy;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import android.app.Activity;
  7. import android.os.Bundle;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.view.ViewGroup.LayoutParams;
  12. import android.widget.Button;
  13. import android.widget.ListView;
  14. import android.widget.PopupWindow;
  15. import android.widget.SimpleAdapter;
  16. /**
  17. *
  18. * @author yulongfei
  19. * @blog blog.csdn.net/zgyulongfei
  20. *
  21. */
  22. public class PopupWindowDemoActivity extends Activity {
  23. private Button button;
  24. private PopupWindow popupWindow;
  25. private ListView listView;
  26. @Override
  27. public void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.main);
  30. initControls();
  31. }
  32. private void initControls() {
  33. LayoutInflater inflater = LayoutInflater.from(this);
  34. View view = inflater.inflate(R.layout.listview_demo, null);
  35. SimpleAdapter adapter = new SimpleAdapter(this, getData(),
  36. R.layout.item,
  37. new String[] { "text" },
  38. new int[] { R.id.item });
  39. listView = (ListView) view.findViewById(R.id.listview);
  40. listView.setAdapter(adapter);
  41. //自适配长、框设置
  42. popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT,
  43. LayoutParams.WRAP_CONTENT);
  44. popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg));
  45. popupWindow.setOutsideTouchable(true);
  46. popupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
  47. popupWindow.update();
  48. popupWindow.setTouchable(true);
  49. popupWindow.setFocusable(true);
  50. button = (Button) findViewById(R.id.button);
  51. button.setOnClickListener(new OnClickListener() {
  52. @Override
  53. public void onClick(View v) {
  54. if (!popupWindow.isShowing()) {
  55. popupWindow.showAsDropDown(button, 0, 0);
  56. }
  57. }
  58. });
  59. }
  60. private List<Map<String, String>> getData() {
  61. List<Map<String, String>> list = new ArrayList<Map<String, String>>();
  62. Map<String, String> map = new HashMap<String, String>();
  63. map.put("text", "中国");
  64. list.add(map);
  65. map = new HashMap<String, String>();
  66. map.put("text", "加油");
  67. list.add(map);
  68. map = new HashMap<String, String>();
  69. map.put("text", "钓鱼岛是中国的");
  70. list.add(map);
  71. map = new HashMap<String, String>();
  72. map.put("text", "!!");
  73. list.add(map);
  74. return list;
  75. }
  76. }

运行结果图如下所示:

咦?不是已经设置自适应长和宽了吗?为什么显示出来的效果还是占满屏幕的宽度呢?

可以看看stackoverflow上面这个人问的问题,这个问题想必纠结了挺多人。虽然我不知道具体的原因是什么,但是我有个解决的方案,我也同时在stackoverflow上做了解答,下面我具体来说明一下。

为了让PopupWindow能够自适应ListView的内容,需要在listview_demo.xml添加一项:

[html] view
plain
copy

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <ListView android:id="@+id/listview"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent" />
  8. <TextView android:layout_width="wrap_content"
  9. android:layout_height="0dp"
  10. android:textSize="18sp"
  11. android:text="钓鱼岛是中国的" />
  12. </LinearLayout>

先看显示结果再做解释:

看到了吗?很神奇吧,popupwindow的宽度进行了自适配。

因为我在xml中加了一个TextView,然后设置了高度为0,这样他就看不到了。

最重要的步骤是我在TextView中设置了android:text="钓鱼岛是中国的",这一句是关键性的动作。

因为TextView才是自适配的砝码,要在text中写上你的listView中最长的那个字符。上述demo中,所有显示的文字{中国,加油,钓鱼岛是中国的,!!!}中”钓鱼岛是中国的“是最长的。

虽然方法不太好,但是实现了效果。如果你遇到这样的问题,可以试试这种方式。

希望本文能够帮到有需要的朋友!

点击下载本文Demo。

popupwindow和listview的更多相关文章

  1. PopupWindow+ListView+OnItemClick点击无效

    昨天踩了个大坑,从下午折腾到现在.实现以下功能: popupWindow显示listview,listView OnItemClick点击后获取值. 由于重写listview 是有两部分 列表正文和右 ...

  2. android 三种弹出框之一PopupWindow

    PopupWindow 在android的弹出框我目前了解到的是有三种:AlertDialog,PopupWindow,Activity伪弹框, AlertDialog太熟悉了,这里就不介绍了 就先看 ...

  3. PopupWindow的使用以及ArrayAdatper.notifyDataSetChanged()无效详解

    Android的对话框有两种:PopupWindow和AlertDialog.它们的不同点在于: AlertDialog的位置固定,而PopupWindow的位置可以随意 AlertDialog是非阻 ...

  4. PopupWindow下拉列表

    效果图 步骤: 1.画出编辑框的布局.popupWindow的布局.popupWindow中listview每行的布局 2.new一个PopupWindow对象,设置其属性 3.定义一个BaseAda ...

  5. 弹出PopupWindow背景变暗的实现

    弹出PopuoWindow后 代码里设置的是PopupWindow默认获取焦点 所以PopupWindow显示的时候其它控件点击是没有反应的 用到的方法是 pwMyPopWindow.setFocus ...

  6. Android下拉选择框之PopupWindow

    1.效果图 2.思路分析 1.点击弹出对话框 popupwindow 2.对popupwindow进行相关设置,popupwindow中设置view为listview 3.listview中item设 ...

  7. Android笔记——Android自定义控件

    目录: 1.自定义控件概述 01_什么是自定义控件 Android系统中,继承Android系统自带的View或者ViewGroup控件或者系统自带的控件,并在这基础上增加或者重新组合成我们想要的效果 ...

  8. Android之仿微信图片选择器

    先上效果图.第一张图显示的是“相机”文件夹中的所有图片:通过点击多张图片可以到第二张图所示的效果(被选择的图片会变暗,同时选择按钮变亮):点击最下面的那一栏可以到第三张图所示的效果(显示手机中所有包含 ...

  9. Android自定义spinner下拉框实现的实现

    一:前言 本人参考博客:http://blog.csdn.net/jdsjlzx/article/details/41316417 最近在弄一个下拉框,发现Android自带的很难实现我的功能,于是去 ...

随机推荐

  1. Postgres中postmaster代码解析(中)

    今天我们对postmaster的以下细节进行讨论: backend的启动和client的连接请求的认证 客户端取消查询时的处理 接受pg_ctl的shutdown请求进行shutdown处理 2.与前 ...

  2. .htaccess rewrite 规则详细说明

    rewrite的语法格式: RewriteEngine On #要想rewrite起作用,必须要写上哦 RewriteBase url-path #设定基准目录,例如希望对根目录下的文件rewrtie ...

  3. C++ 二分法求解方程的解

    二分法是一种求解方程近似根的方法.对于一个函数 f(x)f(x),使用二分法求 f(x)f(x) 近似解的时候,我们先设定一个迭代区间(在这个题目上,我们之后给出了的两个初值决定的区间 [-20,20 ...

  4. Linux学习之CentOS(十七)-----释放 Linux 系统预留的硬盘空间 与Linux磁盘空间被未知资源耗尽 (转)

    释放 Linux 系统预留的硬盘空间  大多数文件系统都会保留一部分空间留作紧急情况时用(比如硬盘空间满了),这样能保证有些关键应用(比如数据库)在硬盘满的时候有点余地,不致于马上就 crash,给监 ...

  5. Spring中<context:annotation-config/>

    最近在研究Spring中<context:annotation-config/>配置的作用,现记录如下: <context:annotation-config/>的作用是向Sp ...

  6. java.lang.NumberFormatException: For input string: " "

    原因:这个异常是说,在将字符串""转换为number的时候格式化错误.额,很简单的异常,以前我是写个方法,然后遍历对比不正确的数字或者用正则表达式之类的.现在发现一个很漂亮的方法, ...

  7. 轻松理解AOP问题

    先说一个Spring是什么吧,大家都是它是一个框架,但框架这个词对新手有点抽象,以致于越解释越模糊,不过它确实是个框架的,但那是从功能的角度来定义的,从本质意义上来讲,Spring是一个库,一个Jav ...

  8. Why Helm? - 每天5分钟玩转 Docker 容器技术(160)

    本章我们将学习 Helm,Kubernetes 的包管理器. 每个成功的软件平台都有一个优秀的打包系统,比如 Debian.Ubuntu 的 apt,Redhat.Centos 的 yum.而 Hel ...

  9. easyui datagrid属性和方法

    本文可以当做api来使用 使用方法(Usage Example) 从现有的表单元素创建数据表格,定义在html中的行,列和数据. <table class="easyui-datagr ...

  10. java web中filter分析

    摘自博客园,博主孤傲苍狼 一.Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp ...