用過 PopupWindow 的人大概都遇過同一個煩惱:尺寸不好定。寫死像素值,換一台解析度不同的手機就變形;寫滿螢幕,又失去彈出視窗該有的輕巧感。
直覺做法是讓它依內容自適應:
popupWindow.setWidth(LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(LayoutParams.WRAP_CONTENT);
但實測之後你會發現:高度沒問題,寬度卻仍然佔滿整個螢幕。這個坑困擾過不少人,Stack Overflow 上也有相同的提問。
先看主畫面 main.xml,只放一顆觸發用的按鈕:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://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>
彈出視窗的內容 listview_demo.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://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>
清單項目 item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://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>
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;
public class PopupWindowDemoActivity extends Activity {
private Button button;
private PopupWindow popupWindow;
private ListView listView;
@Override
public 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() {
@Override
public 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>>();
String[] texts = { "中國", "加油", "這是最長的一段文字", "!!" };
for (String t : texts) {
Map<String, String> map = new HashMap<String, String>();
map.put("text", t);
list.add(map);
}
return list;
}
}
關鍵在於,ListView 在 WRAP_CONTENT 下不會主動把自己縮到內容寬度,於是 PopupWindow 無從參考。解法是在同一層版面裡多塞一個高度為 0 的 TextView,讓它充當隱形的量尺:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://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>
原理說穿了很樸素:高度設為 0dp,使用者看不到它;但它的 android:text 會參與父容器的寬度計算。只要把清單裡最長的那句話填進去——本例中是「這是最長的一段文字」——父容器就會被撐到足夠寬,PopupWindow 的寬度自然就對了。
這不算優雅,本質上是用一個 dummy 節點去補 ListView 量測的不足,但它有效、改動小,也不影響其他邏輯。若你的清單內容會動態變化,記得同步更新這個 TextView 的字串。遇到相同困擾時,不妨試試這招。