Divider在Android Studio中不用为ListView工作



我正在尝试将间距添加到ListView中的列表项。我尝试使用Divider和DividerHeight,但这似乎不起作用。请什么错?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_export_logistics"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.orume.export.ExportLogisticsActivity">

<include layout="@layout/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></include>

<ListView
    android:id="@+id/ListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:divider="@null"
    android:dividerHeight="16dp"
    android:layout_marginTop="65dp"
    android:background="#fff"></ListView>
</RelativeLayout>

divider.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >
<stroke
    android:width="20dp"
    android:color="#dd0d0d" />
</shape>

您没有将分隔线设置为ListView
android:divider="@null" @null这里意味着您将分隔线设置为空。现在您有了可绘制的divider.xml,您需要告诉Android您希望将其用作分隔线。

如果您的divider.xml放置在drawable目录下,则使用android:divider="@drawable/divider"

好。找到了出路。我最终将android:layout_marginBottom添加到定义我的listView的TexView中,并且效果很好。谢谢。

为您的列表项目创建自定义视图,并在该自定义项目布局中提供顶部和底部填充或其他空间调整,以带来列表项目的空间。例如链接在下面listView的Android自定义行项目

另一个注意事项是给出分隔的高度可能无法正常工作。

<ListView
    android:id="@+id/ListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:divider="@android:color/transparent"
    android:dividerHeight="16dp"
    android:layout_marginTop="65dp"
    android:background="#fff"></ListView>

用您的listView替换此listView ..

最新更新