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

來源:互聯網
上載者:User

======原文地址:blog.csdn.net/zgyulongfei ======


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

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

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

點擊下載本文Demo。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.