歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> PopupWindow中顯示ListView時自適配窗口大小

PopupWindow中顯示ListView時自適配窗口大小

日期:2017/3/1 10:10:11   编辑:Linux編程

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

  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

  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

  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添加一項:

  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中,所有顯示的文字{中國,加油,釣魚島是中國的,!!!}中”釣魚島是中國的“是最長的。

雖然方法不太好,但是實現了效果。如果你遇到這樣的問題,可以試試這種方式。

希望本文能夠幫到有需要的朋友!

PopupWindow中顯示ListView時自適配窗口大小 Demo 下載:

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2012年資料/9月/13日/PopupWindow中顯示ListView時自適配窗口大小

Copyright © Linux教程網 All Rights Reserved