You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Android中ListView的列表项之间设置灰色背景?

Android ListView列表项间背景设置为灰色

我正在用Kotlin开发一款Android应用,以下是当前Activity的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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"
    android:orientation="vertical"
    android:background="@color/white"
    tools:context=".DeviceTypeListActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/headerFragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="it.eresult.silvermountain.HeaderFragment"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintTop_toBottomOf="@id/headerFragmentContainer"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <TextView
            android:id="@+id/text_view_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Associa Dispositivo"
            android:layout_marginVertical="0dp"
            android:padding="0dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:fontFamily="@font/poppins_bold"
            android:textColor="@color/main_green"
            android:textSize="24dp"
            />
        <TextView
            android:id="@+id/text_view_subtitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Seleziona la tipologia di dispositivo
che vuoi associare"
            app:layout_constraintTop_toBottomOf="@id/text_view_title"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:gravity="center"
            android:textColor="@color/dark_grey"
            android:textSize="18sp"
            />

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/about_main_content"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/left_corner_rounded_box"
            android:padding="40dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintEnd_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/text_view_subtitle"
            app:layout_constraintVertical_bias="0.0">

            <ListView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:id="@+id/listView"
                tools:listitem="@layout/device_list_item"
                android:layout_marginHorizontal="0dp"
                android:divider="@android:color/white"
                android:dividerHeight="15dp"
                app:layout_constraintTop_toTopOf="parent"
                android:background="#F0F0F0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent" />


        </androidx.constraintlayout.widget.ConstraintLayout>

        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/footer_fragment_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:name="it.eresult.silvermountain.FooterFragment"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            />


    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

当前效果:
当前效果

需求:将ListView列表项之间的背景设置为灰色,达到如下目标效果:
目标效果


解决方案

方法一:直接修改分隔线颜色

当前ListView的分隔线设置为白色,只需将其改为灰色即可:

<ListView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:id="@+id/listView"
    tools:listitem="@layout/device_list_item"
    android:layout_marginHorizontal="0dp"
    android:divider="@color/dark_grey" <!-- 替换为灰色 -->
    android:dividerHeight="15dp"
    app:layout_constraintTop_toTopOf="parent"
    android:background="#F0F0F0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />

方法二:层级背景实现(更贴合目标效果)

如果目标是列表项为白色、间隔为灰色,推荐这种方式:

  1. 修改ListView的背景为灰色,作为间隔色
  2. 将分隔线设为透明,保留分隔线高度作为间隔
  3. 确保列表项布局device_list_item的背景为白色

修改后的ListView代码:

<ListView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:id="@+id/listView"
    tools:listitem="@layout/device_list_item"
    android:layout_marginHorizontal="0dp"
    android:divider="@android:color/transparent" <!-- 透明分隔线 -->
    android:dividerHeight="15dp" <!-- 间隔高度 -->
    app:layout_constraintTop_toTopOf="parent"
    android:background="@color/dark_grey" <!-- 灰色背景 -->
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />

同时调整列表项布局device_list_item的根背景:

<!-- device_list_item.xml 示例 -->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white" <!-- 列表项白色背景 -->
    android:padding="16dp">

    <!-- 列表项内容 -->

</LinearLayout>

这样设置后,ListView的灰色背景会透过透明分隔线显示,形成列表项之间的灰色间隔,与目标效果一致。


内容的提问来源于stack exchange,提问作者bircastri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 13:33:10