gpt4 book ai didi

android - Listview 未包装的弹出窗口

转载 作者:行者123 更新时间:2023-11-30 02:55:01 26 4
gpt4 key购买 nike

我正在尝试使用 ListView 实现一个弹出窗口。当我在 ListView 中添加项目时,它会填满 anchor View 下方的屏幕。

基本上我的 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

最佳答案

您可以测量适配器内容中最大 View 的宽度,并以此宽度启动弹出窗口。

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

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);

关于android - Listview 未包装的弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23427652/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com