我将 ListView 膨胀为 PopupWindow 中的 contentView。 如果我不设置宽度和高度,我就看不到 PopupWindow。 如果我这样设置它们:
setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
布局设置为“fill_parent”。为什么?
ListView和ListView item的布局属性都设置为“wrap_content”。 有什么建议吗?谢谢。
最佳答案
这是怎么做的:
// Don't use this. It causes ListView's to have strange widths, similar to "fill_parent"
//popupWindow.setWindowLayoutMode(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
// Instead, use this:
final int NUM_OF_VISIBLE_LIST_ROWS = 4;
listView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
popupWindow.setWidth(listView.getMeasuredWidth());
popupWindow.setHeight((listView.getMeasuredHeight() + 10) * NUM_OF_VISIBLE_LIST_ROWS);
请注意,setWidth()
和 setHeight()
使用原始像素值,因此您需要针对不同的屏幕尺寸和不同的密度进行调整。
关于android - 如何在 Android 的 PopupWindow 中设置 ListView 的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4085093/