最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - Set background color between two items of ListView - Stack Overflow

programmeradmin1浏览0评论

I m building an Android application using Kotlin. So this is the layout of my Activity

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android=";
    xmlns:app=";
    xmlns: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>

This is the output

Now I need to set gray color of background between the items of ListView:

I m building an Android application using Kotlin. So this is the layout of my 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>

This is the output

Now I need to set gray color of background between the items of ListView:

Share Improve this question asked Feb 7 at 10:51 bircastribircastri 2,15914 gold badges55 silver badges131 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

In your ListView change the divider color

            android:divider="#F0F0F0" //grey color
            android:dividerHeight="15dp"
            

Create a new XML file,

res/drawable/list_divider.xml

Apply it in your ListView:

<shape android:shape="rectangle">
    <size android:height="15dp"/>  <!-- Adjust thickness -->
    <solid android:color="#D3D3D3"/>  <!-- Light gray -->
</shape>
发布评论

评论列表(0)

  1. 暂无评论