popupwindow和listview
在使用PopupWindow的时候,有一个不好的地方就是不太好设置弹出窗体的大小。如果指定绝对大小,那么对于不同分辨率不同尺寸的手机来说,显示出来效果会不同,从而导致用户体验不佳。
为了达到PopupWindow能够自适配布局大小,可以在设置长宽时候指定:
- popupWindow.setWidth(LayoutParams.WRAP_CONTENT);
- popupWindow.setHeight(LayoutParams.WRAP_CONTENT);
下面我就来具体讲解一下在PopupWindow中使用ListView的方法。
首先贴出的是main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <Button android:id="@+id/button"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="弹出popupWindow" />
- </LinearLayout>
然后贴出的是PopupWindow中显示的listview_demo.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <ListView android:id="@+id/listview"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- </LinearLayout>
再贴出的是listview显示的每一项item.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView android:id="@+id/item"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="18sp" />
- </LinearLayout>
最后贴出的是java代码PopupWindowDemoActivity.java
- package xmu.zgy;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.Button;
- import android.widget.ListView;
- import android.widget.PopupWindow;
- import android.widget.SimpleAdapter;
- /**
- *
- * @author yulongfei
- * @blog blog.csdn.net/zgyulongfei
- *
- */
- public class PopupWindowDemoActivity extends Activity {
- private Button button;
- private PopupWindow popupWindow;
- private ListView listView;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- initControls();
- }
- private void initControls() {
- LayoutInflater inflater = LayoutInflater.from(this);
- View view = inflater.inflate(R.layout.listview_demo, null);
- SimpleAdapter adapter = new SimpleAdapter(this, getData(),
- R.layout.item,
- new String[] { "text" },
- new int[] { R.id.item });
- listView = (ListView) view.findViewById(R.id.listview);
- listView.setAdapter(adapter);
- //自适配长、框设置
- popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT,
- LayoutParams.WRAP_CONTENT);
- popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg));
- popupWindow.setOutsideTouchable(true);
- popupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
- popupWindow.update();
- popupWindow.setTouchable(true);
- popupWindow.setFocusable(true);
- button = (Button) findViewById(R.id.button);
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- if (!popupWindow.isShowing()) {
- popupWindow.showAsDropDown(button, 0, 0);
- }
- }
- });
- }
- private List<Map<String, String>> getData() {
- List<Map<String, String>> list = new ArrayList<Map<String, String>>();
- Map<String, String> map = new HashMap<String, String>();
- map.put("text", "中国");
- list.add(map);
- map = new HashMap<String, String>();
- map.put("text", "加油");
- list.add(map);
- map = new HashMap<String, String>();
- map.put("text", "钓鱼岛是中国的");
- list.add(map);
- map = new HashMap<String, String>();
- map.put("text", "!!");
- list.add(map);
- return list;
- }
- }
运行结果图如下所示:

咦?不是已经设置自适应长和宽了吗?为什么显示出来的效果还是占满屏幕的宽度呢?
可以看看stackoverflow上面这个人问的问题,这个问题想必纠结了挺多人。虽然我不知道具体的原因是什么,但是我有个解决的方案,我也同时在stackoverflow上做了解答,下面我具体来说明一下。
为了让PopupWindow能够自适应ListView的内容,需要在listview_demo.xml添加一项:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <ListView android:id="@+id/listview"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- <TextView android:layout_width="wrap_content"
- android:layout_height="0dp"
- android:textSize="18sp"
- android:text="钓鱼岛是中国的" />
- </LinearLayout>
先看显示结果再做解释:

看到了吗?很神奇吧,popupwindow的宽度进行了自适配。
因为我在xml中加了一个TextView,然后设置了高度为0,这样他就看不到了。
最重要的步骤是我在TextView中设置了android:text="钓鱼岛是中国的",这一句是关键性的动作。
因为TextView才是自适配的砝码,要在text中写上你的listView中最长的那个字符。上述demo中,所有显示的文字{中国,加油,钓鱼岛是中国的,!!!}中”钓鱼岛是中国的“是最长的。
虽然方法不太好,但是实现了效果。如果你遇到这样的问题,可以试试这种方式。
希望本文能够帮到有需要的朋友!
popupwindow和listview的更多相关文章
- PopupWindow+ListView+OnItemClick点击无效
昨天踩了个大坑,从下午折腾到现在.实现以下功能: popupWindow显示listview,listView OnItemClick点击后获取值. 由于重写listview 是有两部分 列表正文和右 ...
- android 三种弹出框之一PopupWindow
PopupWindow 在android的弹出框我目前了解到的是有三种:AlertDialog,PopupWindow,Activity伪弹框, AlertDialog太熟悉了,这里就不介绍了 就先看 ...
- PopupWindow的使用以及ArrayAdatper.notifyDataSetChanged()无效详解
Android的对话框有两种:PopupWindow和AlertDialog.它们的不同点在于: AlertDialog的位置固定,而PopupWindow的位置可以随意 AlertDialog是非阻 ...
- PopupWindow下拉列表
效果图 步骤: 1.画出编辑框的布局.popupWindow的布局.popupWindow中listview每行的布局 2.new一个PopupWindow对象,设置其属性 3.定义一个BaseAda ...
- 弹出PopupWindow背景变暗的实现
弹出PopuoWindow后 代码里设置的是PopupWindow默认获取焦点 所以PopupWindow显示的时候其它控件点击是没有反应的 用到的方法是 pwMyPopWindow.setFocus ...
- Android下拉选择框之PopupWindow
1.效果图 2.思路分析 1.点击弹出对话框 popupwindow 2.对popupwindow进行相关设置,popupwindow中设置view为listview 3.listview中item设 ...
- Android笔记——Android自定义控件
目录: 1.自定义控件概述 01_什么是自定义控件 Android系统中,继承Android系统自带的View或者ViewGroup控件或者系统自带的控件,并在这基础上增加或者重新组合成我们想要的效果 ...
- Android之仿微信图片选择器
先上效果图.第一张图显示的是“相机”文件夹中的所有图片:通过点击多张图片可以到第二张图所示的效果(被选择的图片会变暗,同时选择按钮变亮):点击最下面的那一栏可以到第三张图所示的效果(显示手机中所有包含 ...
- Android自定义spinner下拉框实现的实现
一:前言 本人参考博客:http://blog.csdn.net/jdsjlzx/article/details/41316417 最近在弄一个下拉框,发现Android自带的很难实现我的功能,于是去 ...
随机推荐
- I/O控制的主要功能
主要功能: 1. 解释用户的I/O系统调用.将用户I/O系统调用转换为I/O控制模块认识的命令模式. 2. 设备驱动.根据得到的I/O命令,启动物理设备完成指定的I/O操作. 3. 中断处理.对 ...
- 如何使用 TeamViewer 配置QuickConnect按钮?
QuickConnect作为TeamViewer中一个比较重要的部分,得到了很多用户的认可.那么在实际运用中,怎么才能设置网页或单个程序的QuickConnect呢?所以小编以此问题为例,教大家如何配 ...
- 解决Mysql数据库拒绝远程连接和忘记密码的问题
解决数据库忘记密码的问题 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 编辑m ...
- jdk和tomcat配置
1.一次成功的JAVA环境变量配置,必须要配置一下三个系统变量:JAVA_HOME(变量值为JDK的路径),PATH(变量值:%JAVA_HOME%\bin;),CLASS_PATH(变量值为JDK中 ...
- 牛客网编程练习之PAT乙级(Basic Level):1041 说反话
直接分隔取反即可 AC代码: import java.util.Scanner; /** * @author CC11001100 */ public class Main { public stat ...
- 学习在.NET Core中使用RabbitMQ进行消息传递之持久化(二)
前言 上一节我们简单介绍了RabbitMQ和在安装后启动所出现的问题,本节我们开始正式进入RabbitMQ的学习,对于基本概念请从官网或者其他前辈博客上查阅,我这里不介绍基础性东西,只会简单提一下,请 ...
- NLP系列(5)_从朴素贝叶斯到N-gram语言模型
作者: 龙心尘 && 寒小阳 时间:2016年2月. 出处: http://blog.csdn.net/longxinchen_ml/article/details/50646528 ...
- Github Atom开源文本代码编辑器- 由 Github 打造的下一代编程开发利器
个人理解:Github 热度超凡的一个项目Atom,electron是整个atom的核心,对于electron可以理解成 electron =io.js + Chromium 通过 Electr ...
- Mongo 整体架构介绍(1)-------分片集群
摘要 在mongo初识文中介绍了mongo与cassandra的主要区别,以及mongo物理部署架构图.本文接着上一篇的mongo 架构图,来继续讲分片集群. 分片介绍 shard key mongo ...
- ROS_Kinetic_28 turtlebot gazebo demo例子
ROS_Kinetic_28 turtlebot gazebo demo例子 官方教程:http://wiki.ros.org/turtlebot_gazebo/Tutorials/indigo/Ma ...