【问题标题】:Android ListView with default ArrayAdapter is oversized when displayed显示时具有默认 ArrayAdapter 的 Android ListView 过大
【发布时间】:2017-08-29 23:28:28
【问题描述】:

API 级别:17

我无法在文件浏览应用程序中更改 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

但是,不幸的是,在选项列表之后仍然有大量的额外空间:

我尝试设置自定义样式并将其用作布局和列表视图的样式,编译器没有抱怨,但它对窗口绝对没有影响。有没有办法智能地调整它的大小?

【问题讨论】:

  • 您是否尝试过使用 LinearLayout 而不是 RelativeLayout 作为 listView 的根布局。我之前体验过相对布局使用 match_parent 是否使用 wrap_content。如果使用 LinearLayout 不能解决您的问题,我建议您测量 listView 项的文本宽度并使用像素指定宽度,但这需要太多工作。
  • 嗯,使用 LinearLayout 是合理的,但遗憾的是没有奏效。我现在将尝试使用更笨重的文本测量选项。谢谢你的回答!

标签: android listview android-arrayadapter android-popupwindow


【解决方案1】:

对于将来可能会为此苦苦挣扎的人 - 我找到了一个我以前从未见过的答案,这对我有帮助: 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);

这会产生以下结果:

2ez2pz

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 2015-08-06
    • 2016-07-30
    相关资源
    最近更新 更多