在使用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
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<listview android:id="@+id/listview"
android:layout_height="match_parent" />
再贴出的是listview显示的每一项item.xml
<textview android:id="@+id/item"
android:layout_width="wrap_content"
android:textsize="18sp" />
最后贴出的是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;
@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>>();
map<string, string> map = new hashmap<string, string>();
map.put("text", "中国");
list.add(map);
map = new hashmap<string, string>();
map.put("text", "加油");
map.put("text", "钓鱼岛是中国的");
map.put("text", "!!");
return list;
}
运行结果图如下所示:

咦?不是已经设置自适应长和宽了吗?为什么显示出来的效果还是占满屏幕的宽度呢?
为了让popupwindow能够自适应listview的内容,需要在listview_demo.xml添加一项:
<textview android:layout_width="wrap_content"
android:layout_height="0dp"
android:textsize="18sp"
android:text="钓鱼岛是中国的" />
先看显示结果再做解释:
看到了吗?很神奇吧,popupwindow的宽度进行了自适配。
因为我在xml中加了一个textview,然后设置了高度为0,这样他就看不到了。
最重要的步骤是我在textview中设置了android:text="钓鱼岛是中国的",这一句是关键性的动作。
因为textview才是自适配的砝码,要在text中写上你的listview中最长的那个字符。上述demo中,所有显示的文字{中国,加油,钓鱼岛是中国的,!!!}中”钓鱼岛是中国的“是最长的。
虽然方法不太好,但是实现了效果。如果你遇到这样的问题,可以试试这种方式。
希望本文能够帮到有需要的朋友!
<a target="_blank" href="http://download.csdn.net/detail/zgyulongfei/4566716">点击下载本文demo。</a>