最近开发使用PopupWindow弹框显示ListView,开始使用的固定宽度和高度显示,由于数据量不固定,数据少时会有空白显示,于是改成使用wrap_content,发现了一些之前没注意到的问题,记录下来:
1、必须设置PopupWindow的Height、Width
不设置的话默认都为0,什么都不会显示。
在Android中,控件的大小,都是根据父控件大小确定的。给PopupWindow设置的contentView一般都是inflate得到的,且root为null,没有父布局所以无法计算大小,因此必须设置width和height。
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.popwindow, null);
PopupWindow popupWindow = new PopupWindow(view,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
popwindow.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.andro