ListView的项目不占用整个设备空间

时间:2015-11-22 20:18:04

标签: android listview android-listview

我的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);
    }

预期输出 enter image description here

实际输出: enter image description here

3 个答案:

答案 0 :(得分:0)

如果你有一个固定的项目数,你不需要一个listView,但是你需要一个带有7个TextView的简单线性布局,并为Textviews高度设置一个权重,如:

^[0-9]+[\s]

答案 1 :(得分:0)

您可以创建一个自定义适配器来测量设备屏幕的高度并传播项目

<强> activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:id="@+id/linear_layout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              tools:context=".DaysList">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView"/>
</LinearLayout>

<强> MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final ListView listView = (ListView) findViewById(R.id.listView);
    listView.setBackgroundColor(Color.GREEN);

    String[] values = new String[]{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

    final ArrayAdapter<String> adapter = new Adapter(this, android.R.layout.simple_list_item_1, values);
    listView.setAdapter(adapter);
}

<强> Adapter.java

public class Adapter extends ArrayAdapter<String> {

    Context context;
    public Adapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        this.context = context;
    }

    public Adapter(Context context, int resource, String[] items) {
        super(context, resource, items);
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        int fullHeight = parent.getHeight();
        int itemHeight = fullHeight / getCount() ;

        ViewGroup.LayoutParams lp = view.getLayoutParams();
        lp.height = itemHeight;
        view.setLayoutParams(lp);

        return view;
    }
}

答案 2 :(得分:0)

默认情况下,ListView包含ScrollView,它具有无限高度。因此,即使您只将listView声明为包装内容,只要您的示例中存在空白空间,它就会填写整个内容。您可以在设置适配器后调用此方法来修复它:

public boolean setListViewHeight(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter != null) {

        int numberOfItems = listAdapter.getCount();

        View item = listAdapter.getView(0, null, listView);
        item.measure(0, 0);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, numberOfItems * item.getMeasuredHeight());
        listView.setLayoutParams(params);

        return true;

    } else {
        return false;
    }
}

在:

enter image description here

后:

enter image description here