天天看點

解決PopupWindow中顯示ListView時不能自适配視窗大小的問題

在使用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.  

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