【发布时间】:2015-11-22 20:18:04
【问题描述】:
我的 listView 包含 7 个项目。我希望这 7 个项目覆盖整个设备空间。
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".DaysList">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
android:layout_gravity="center_vertical"
>
</ListView>
</LinearLayout>
我的 onCreate() 方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
ListView listView=getListView();
listView.setBackgroundColor(Color.GREEN);
String[] values = new String[] { "Monday", "Tuesday", "Wednesday",
"Thursday","Friday", "Saturday","Sunday" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
【问题讨论】:
-
如果你总是有 7 个项目,你应该使用带有 layout_weight 的垂直 LinearLayout 而不是 ListView
-
您是否尝试将 listview 的 layout_height 从“match_parent”更改为“wrap_content”?
-
@fractalwrench 我使用了 android:orientation=vertical,结果还是一样。
-
@OrkunKoçyiğit 是的,我试过了
-
ListView 不能以这种方式工作,因为您可以在可见屏幕上平等地放置所有项目。您必须使用其他 Layout 而不是 ListView 来实现这一点。就像将 7 个 TextViews 放在一个 LinearLayout 中并设置所有 TextView 的
layout_weight=1
标签: android listview android-listview