显示时,带有默认ArrayAdapter的Android ListView超大

时间:2017-08-29 23:28:28

标签: android listview android-arrayadapter android-popupwindow

API级别:17

我在更改文件浏览应用中ListView的默认大小时遇到​​问题。

这仍然只是一个原型(参见背景颜色),但弹出窗口内的列表效果非常好!除了它太大并且拒绝调整大小以包装内容而不指定宽度的设置像素大小。列表的高度没问题,但宽度都错了。从其他问题来看,这些解决方案并没有为我工作。

我的listview最初是用XML定义的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/rootSelectionPane">
    <ListView
        android:id="@+id/selectionList"
        android:background="@color/ruddy"
        android:windowIsFloating="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </ListView>
</RelativeLayout>

popupwindow / listview / arrayadapter实例化如下:

@Override public void onCreate(Bundle savedInstanceState) {

    // Other setup stuff ...

    LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);

    mPopupView = layoutInflater.inflate(R.layout.file_selected_popup, null);

    mSelectListWindow = new PopupWindow(mPopupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    mSelectListWindow.setBackgroundDrawable(new BitmapDrawable());

    mSelectListWindow.setOutsideTouchable(true);

    ListView listView = (ListView) mPopupView.findViewById(R.id.selectionList);

    String[] fileOptions = new String[] {
            "Cut",
            "Copy",
            "Delete",
            "Rename"
    };

    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_selectable_list_item, fileOptions);

    listView.setAdapter(arrayAdapter);
    // more setup stuff after

但是,在选项列表之后还有一个不幸的额外空间:

screencap

我已经尝试设置自定义样式并将其用作布局和列表视图的样式,并且编译器不会抱怨,但它对窗口完全没有影响。有没有办法聪明地调整它?

1 个答案:

答案 0 :(得分:0)

对于将来可能会遇到这种情况的人们 - 我找到了一个我之前没见过的答案,这对我有帮助: Change ListView divider length based on longest Text in List

我将上面的代码部分更改为以下内容(仍在onCreate中)

LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
mPopupView = layoutInflater.inflate(R.layout.file_selected_popup, null);
mSelectListWindow = new PopupWindow(mPopupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mSelectListWindow.setBackgroundDrawable(new BitmapDrawable());
mSelectListWindow.setOutsideTouchable(true);

ListView listView = (ListView) mPopupView.findViewById(R.id.selectionList);
String[] fileOptions = new String[] {
    "Cut",
    "Copy",
    "Delete",
    "Rename"
};
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_selectable_list_item, fileOptions);
listView.setAdapter(arrayAdapter);

ListAdapter listAdapter = listView.getAdapter();
int longestWidth = listAdapter.getView(0, null, listView).getMeasuredWidth();
for (int i=0; i < listAdapter.getCount(); i++) {
    View listItem = listAdapter.getView(i, null, listView);
    listItem.measure(0, 0);
    int width = listItem.getMeasuredWidth();
    if (width > longestWidth) {
        longestWidth = width;
    }
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.width = longestWidth;
listView.setLayoutParams(params);

产生以下结果:

screen capture

2ez2pz