popupwindow和listview

简介: <p style="color:rgb(54,46,43); font-family:Arial; font-size:14px; line-height:26px"> 在使用PopupWindow的时候,有一个不好的地方就是不太好设置弹出窗体的大小。如果指定绝对大小,那么对于不同分辨率不同尺寸的手机来说,显示出来效果会不同,从而导致用户体验不佳。</p> <p style="colo

在使用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.   
  6.     <Button android:id="@+id/button"   
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="wrap_content"   
  9.         android:text="弹出popupWindow" />  
  10.   
  11. </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.   
  6.     <ListView android:id="@+id/listview"   
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="match_parent" />  
  9.   
  10. </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.   
  6.     <TextView android:id="@+id/item"  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"   
  9.         android:textSize="18sp" />  
  10.   
  11. </LinearLayout>  


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

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



运行结果图如下所示:



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

可以看看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.   
  6.     <ListView android:id="@+id/listview"   
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="match_parent" />  
  9.   
  10.     <TextView android:layout_width="wrap_content"  
  11.         android:layout_height="0dp"   
  12.         android:textSize="18sp"   
  13.         android:text="钓鱼岛是中国的" />  
  14. </LinearLayout>  

先看显示结果再做解释:



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

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

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

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

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

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

点击下载本文Demo。

目录
相关文章
|
11月前
|
API Android开发 容器
PopupWindow
PopupWindow
102 0
|
Java API Android开发
ListView简单实用
自定义BaseAdapter,绑定ListView的最简单例子
128 0
C#-ListView的使用
ListView顾名思义用来做列表数据展示,也是我们在开发中经常使用的控件之一,接下来将展示下它的一些使用场景,以满足不同的需求。
120 0
|
C#
C#-ListView
C# ListView
83 0
Flutte 之 ListView
Flutte 之 ListView
59 0
Flutte 之 ListView
|
XML Android开发 数据格式
ListView
在这里给大家分享Android的ListView控件的一些经验,能力有限,还望大家多多指教,不喜勿喷哦,kensoon918@163.com only for feedback
1519 0
|
Android开发 数据格式 XML
ListView的使用
ListView样式设置 取消Item点击变色效果 android:listSelector=”@android:color/transparent” Item条目高度的设定 ...
783 0
|
XML Java Android开发
popupwindow和listview
在使用PopupWindow的时候,有一个不好的地方就是不太好设置弹出窗体的大小。如果指定绝对大小,那么对于不同分辨率不同尺寸的手机来说,显示出来效果会不同,从而导致用户体验不佳。 为了达到PopupWindow能够自适配布局大小,可以在设置长宽时候指定: [java] view plaincopy popupWindow.set
1315 0
|
移动开发 数据库 Android开发
Android控件开发——ListView
上篇博客解决了Android客户端通过WebService与服务器端程序进行交互的问题,这篇博客重点关注两个问题,一个是Android应用程序如何与本机文件型数据库SQLite进行交互,另一问题则是如何在ListView中按照我们想要的界面效果进行展示。
823 0