Android Listview删除项目之间的任何空格

时间:2014-06-23 18:09:20

标签: java android listview space

我想删除ListView中不同项之间的任何空格。 代码:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:id="@+id/wrapper"
    android:padding="0dp"
    android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/comment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingLeft="10dip"
            android:paddingRight="10dip"
            android:textColor="#000000"/>

</LinearLayout>

和Listview

<ListView
        android:id="@+id/listView1"
        android:transcriptMode="alwaysScroll"
        android:stackFromBottom="true"
        android:dividerHeight="0dp"
        android:divider="@null"
        android:listSelector="@android:color/transparent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

但物品之间仍有一些空间。任何人都可以帮助我吗?

Screenshot

2 个答案:

答案 0 :(得分:11)

行android:dividerHeight =&#34; 10dp&#34;导致你的线之间的差距。我对整体用户界面进行了颜色编码:

Color coded UI

一旦我在&#34; 10dp&#34;上设置了dividerHeight线。 to&#34; 0dp&#34;我明白了:

Color coded- no dividerHeight

好的,所以这里是我使用的完整代码集,以便您可以看到出错的地方。

MainActivity:

package com.ds.listviewtest;

import android.app.ListActivity;
import android.content.Context;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

public class MainActivity extends ListActivity {

    static final String[] FRUITS = new String[] {
            "Apple", "Avocado", "Banana",
            "Blueberry", "Coconut", "Durian", "Guava", "Kiwifruit",
            "Jackfruit", "Mango", "Olive", "Pear", "Sugar-apple"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setListAdapter(new SimpleAdapter(FRUITS));
        setContentView(R.layout.activity_main);
    }

    private class SimpleAdapter implements ListAdapter
    {
        String[] items;

        public SimpleAdapter(String[] items)
        {
            this.items = items;
        }

        @Override
        public void registerDataSetObserver(DataSetObserver observer) {
            // TODO Auto-generated method stub

        }

        @Override
        public void unregisterDataSetObserver(DataSetObserver observer) {
            // TODO Auto-generated method stub

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return items.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return items[position];
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            View rowView = inflater.inflate(R.layout.list_item, parent, false);
            TextView textView = (TextView) rowView.findViewById(R.id.comment);
            textView.setText(items[position]);

            return rowView;
        }

        @Override
        public int getItemViewType(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public int getViewTypeCount() {
            // TODO Auto-generated method stub
            return 1;
        }

        @Override
        public boolean isEmpty() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean areAllItemsEnabled() {
            // TODO Auto-generated method stub
            return true;
        }

        @Override
        public boolean isEnabled(int position) {
            // TODO Auto-generated method stub
            return true;
        }

    }

}

这是list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:background="@android:color/holo_orange_dark"
    android:id="@+id/wrapper"
    android:padding="0dp"
    android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/comment"
            android:background="@android:color/holo_blue_light"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingLeft="10dip"
            android:paddingRight="10dip"
            android:text="This is a test comment"
            android:textColor="#000000"/>

</LinearLayout>

最后这里是activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_red_light"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@android:color/holo_green_light"
        android:dividerHeight="0dp" />  <!-- EDIT THIS VALUE HERE TO 0DP -->

</RelativeLayout>

答案 1 :(得分:1)

简单地说,将分隔符高度设为负值 示例:
android:dividerHeight="-20dp"
这将删除ListView值之间的空格。