问题
I am having a list view and its list items are generated using Array List and it creates unecessary white space which occupies much space.
How do I remove it from the list items?
Here is my list view:
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="0dp" android:layout_weight="1"
android:divider="@android:color/transparent"
android:dividerHeight="10.0sp"/>
And my list items:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/photo"
android:layout_width="140dip"
android:layout_height="140dip"
android:paddingRight="15dp"
android:paddingLeft="15dp"/>
<TextView android:id="@+id/name"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#000000"
android:paddingTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/photo"/>
<TextView android:id="@+id/itemDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_toRightOf="@id/photo"/>
<TextView android:id="@+id/price"
android:textSize="19sp"
android:textStyle="bold"
android:textColor="#32cd32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/itemDescription"
android:layout_toRightOf="@id/photo"/>
</RelativeLayout>
Screenshot:

回答1:
just remove android:dividerHeight="10.0sp" from xml or make it 1sp or smaller
this may also the problem: android:layout_height="0dp" android:layout_weight="1"
why not giving height as "wrap_content" ? no need to android:layout_weight="1" i think
回答2:
use height wrap-content
for relativelayout in listitems.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</RelativeLayout>
回答3:
Try this for your row, change image and color to yours ..(Use linear)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#123133" >
<ImageView
android:id="@+id/photo"
android:layout_width="140dip"
android:layout_height="140dip"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAAAASDKDJ"
android:textColor="#000000"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="@+id/itemDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAAAASDKDJ" />
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAAAASDKDJ"
android:textColor="#32cd32"
android:textSize="19sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
来源:https://stackoverflow.com/questions/20493480/white-space-around-list-view-items