如何在Android

时间:2016-03-14 16:13:00

标签: android listview popup

我想在textview上的特定位置播放弹出窗口(textview在scrollview中)。我的PopupWindow布局只包含一个列表视图。我使用下面的代码来调整PopupWindow的宽度和高度。 PopupWindow高度正常但宽度不正确。它在不同的设备上显得不同。它也不会扭曲listview内容的宽度。

final PopupWindow attachmentPopup = new PopupWindow(this);
LayoutInflater layoutInflater = getLayoutInflater();
View popupView = layoutInflater.inflate(R.layout.popupwindow, null);
ListView lv = (ListView) popupView.findViewById(R.id.listView);
final ArrayList<CustomItem> popupItems = new ArrayList<CustomItem>();
final CustomAdapter popupAdapter = new CustomAdapter(popupItems, Details.this);
CustomItem item;
item = new CustomItem();
item.setName("Apple");
popupItems.add(item);
item = new CustomItem();
item.setName("Orange");        
popupItems.add(item);
lv.setAdapter(popupAdapter);
int popupWidth =  ViewGroup.LayoutParams.WRAP_CONTENT;
int popupHeight = ViewGroup.LayoutParams.WRAP_CONTENT;
attachmentPopup.setFocusable(true);
attachmentPopup.setWidth(popupWidth);
attachmentPopup.setHeight(popupHeight);
attachmentPopup.setContentView(popupView);
attachmentPopup.setBackgroundDrawable(new BitmapDrawable());
attachmentPopup.showAtLocation(popupView, Gravity.TOP | Gravity.LEFT, mx, my);

popupwindow.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#9acd32"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

请你试试替换

int popupWidth =  ViewGroup.LayoutParams.WRAP_CONTENT;
int popupHeight = ViewGroup.LayoutParams.WRAP_CONTENT;

int popupWidth = WindowManager.LayoutParams.WRAP_CONTENT;
int popupHeight = WindowManager.LayoutParams.WRAP_CONTENT;