解决PopupWindow中显示ListView时不能自适配窗口大小的问题

16 篇文章 0 订阅
11 篇文章 0 订阅

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

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

1.popupWindow.setWidth(LayoutParams.WRAP_CONTENT);    
2.popupWindow.setHeight(LayoutParams.WRAP_CONTENT);  
下面我就来具体讲解一下在PopupWindow中使用ListView的方法。


首先贴出的是main.xml

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

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

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

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, 0, 0);  
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添加一项:

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中,所有显示的文字{中国,加油,钓鱼岛是中国的,!!!}中”钓鱼岛是中国的“是最长的。

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

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

PopupWindow中显示ListView时自适配窗口大小  Demo 下载:

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2012年资料/9月/13日/PopupWindow中显示ListView时自适配窗口大小

本篇文章来源于 Linux公社网站(www.linuxidc.com)  原文链接:http://www.linuxidc.com/Linux/2012-09/70384.htm

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值