【发布时间】: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