没有包装Listview的弹出窗口

时间:2014-05-02 11:59:34

标签: android listview android-listview

我正在尝试使用listview实现一个弹出窗口。当我在listview中添加项目时,它正在填充锚点视图下方的屏幕。

基本上我的listview不包装。 以下是我正在使用的图像和代码: -

button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //              ToolTipView toolTipView = new ToolTipView(MainActivity.this, "");
            //              toolTipView.setText("swfwefw wefwe8732ryf89 8yu2fc h8wuijcb8unwi ");
            //              toolTipView.show(v);
            LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
            View popupView = layoutInflater.inflate(R.layout.popupwindoiw, null);

            final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

            ListView btnDismiss = (ListView)popupView.findViewById(R.id.list);
            final ArrayList<String> list = new ArrayList<String>();
            list.add("hello");
            list.add("helloworld");
            list.add("helloexzeo");


            ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.list_row, list);
            btnDismiss.setAdapter(adapter);
            btnDismiss.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {

                    Toast.makeText(MainActivity.this, list.get(arg2), Toast.LENGTH_SHORT).show();
                    popupWindow.dismiss();
                }
            });

            popupWindow.showAsDropDown(v);

        }
    });

popwindow.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/white" >

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

</RelativeLayout>

list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical" />

在图像中,当前listview显示在红色区域,但我希望它在蓝色区域。

我无法使用弹出窗口的自定义宽度和高度,因为我支持多种屏幕尺寸 enter image description here

1 个答案:

答案 0 :(得分:1)

您可以测量适配器内容中最大视图的宽度,并使用此宽度启动popwindow。

您可以使用此方法来衡量适配器内容中的最大视图 -

private int measureContentWidth(ListAdapter listAdapter) {
ViewGroup mMeasureParent = null;
int maxWidth = 0;
View itemView = null;
int itemType = 0;

final ListAdapter adapter = listAdapter;
final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
final int count = adapter.getCount();
for (int i = 0; i < count; i++) {
    final int positionType = adapter.getItemViewType(i);
    if (positionType != itemType) {
        itemType = positionType;
        itemView = null;
    } 

    if (mMeasureParent == null) {
        mMeasureParent = new FrameLayout(mContext);
    } 

    itemView = adapter.getView(i, itemView, mMeasureParent);
    itemView.measure(widthMeasureSpec, heightMeasureSpec);

    final int itemWidth = itemView.getMeasuredWidth();

    if (itemWidth > maxWidth) {
        maxWidth = itemWidth;
    } 
} 

return maxWidth;

}

您可以像这样启动弹出窗口 -

PopupWindow popupWindow = new PopupWindow(popupView,measureContentWidth(adapter), LayoutParams.WRAP_CONTENT);
相关问题