如何在自定义 ListView 中的列表项之间添加间隙(底部边距)?

How can I add gap (margin bottom) between List Items in the Custom ListView?

如何在自定义 ListView 中的列表项之间添加间隙(底部边距)? 在 Android 中,我想在 ListView 中分隔列表项。

查看 Facebook 应用程序中的示例:

这是XML代码,我用它来设计单个列表项。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#bbccf5bb"
android:focusable="false"
android:padding="5dp"

android:layout_marginBottom="50dp"
android:backgroundTintMode="screen"
>

<TextView
    android:layout_width="268dp"
    android:layout_height="wrap_content"
    android:textDirection="rtl"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Some Quote"
    android:id="@+id/txt_quote"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_toRightOf="@+id/author_img"
    android:layout_toEndOf="@+id/author_img"
    android:layout_alignBottom="@+id/author_img"
    android:textColor="#ffffff"
    android:layout_margin="5dp"

    android:padding="5dp"

    />

<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:id="@+id/author_img"
    android:src="@drawable/ali"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="- Some Author"
    android:id="@+id/txt_author"
    android:layout_below="@+id/txt_quote"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginRight="5dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp" />

<RatingBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ratingBar"
    android:numStars="1"
    android:layout_alignTop="@+id/txt_author"
    android:layout_alignLeft="@+id/author_img"
    android:layout_alignStart="@+id/author_img"
    android:layout_marginLeft="25dp"
    android:stepSize="1" />

    </RelativeLayout>

在布局文件中为每一行项目提供边距(android:layout_marginBottom="10dp),例如:

<?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="wrap_content"
    android:background="@drawable/list_row_selector"
    android:padding="8dp"
    android:layout_marginBottom="10dp" > <--added margin between rows


<ImageView
    android:id="@+id/thumbnail"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="8dp" />
 <TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/thumbnail"
    android:layout_toRightOf="@+id/thumbnail"
    android:textSize="@dimen/title"
    android:textStyle="bold" />

请尝试以下结构化代码,而不是您的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="#bbccf5bb"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:padding="5dp" >

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/author_img"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:background="@drawable/ic_launcher" />

            <TextView
                android:id="@+id/txt_quote"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Some Quote" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <RelativeLayout
                android:layout_width="100dp"
                android:layout_height="wrap_content" >

                <RatingBar
                    android:id="@+id/ratingBar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:numStars="1"
                    android:stepSize="1" />
            </RelativeLayout>

            <TextView
                android:id="@+id/txt_author"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:text="-Some Auther" />
        </TableRow>
    </LinearLayout>

</LinearLayout>

如有任何疑问,请询问。