Margin between items in recycler view Android

后端 未结 10 1331
自闭症患者
自闭症患者 2020-12-24 02:06

Hi I am Following this tutorial:

http://www.journaldev.com/10024/android-recyclerview-and-cardview-example-tutorial

Now I am facing a weird issue the mar

相关标签:
10条回答
  • 2020-12-24 02:32

    Use CardView in Recyclerview Item Layout like this :

     <android.support.v7.widget.CardView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:card_view="http://schemas.android.com/tools"
            card_view:cardCornerRadius="10dp"
            app:cardBackgroundColor="#ACACAC"
            card_view:cardElevation="5dp"
            app:contentPadding="10dp"
            card_view:cardUseCompatPadding="true">
    
    0 讨论(0)
  • 2020-12-24 02:32

    Try to add RecyclerView.ItemDecoration

    To know how to do that: How to add dividers and spaces between items in RecyclerView?

    0 讨论(0)
  • 2020-12-24 02:35

    If you want to do it in XML, jus set paddingTopand paddingLeft to your RecyclerView and equal amount of layoutMarginBottom and layoutMarginRight to the item you inflate into your RecyclerView(or vice versa).

    0 讨论(0)
  • 2020-12-24 02:39

    In XML, to manage margins between items of a RecyclerView, I am doing in this way, operating on these attributes of detail layout (I am using a RelativeLayout):

    • android:layout_marginTop
    • android:layout_marginBottom

    of course it is also very important to set these attributes:

    • android:layout_width="wrap_content"
    • android:layout_height="wrap_content"

    Here follows a complete example, this is the layout for the list of items:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.recyclerview.widget.RecyclerView 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:name="blah.ui.meetings.MeetingEventFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="0dp"
    android:layout_marginVertical="2dp"
    app:layoutManager="LinearLayoutManager"
    tools:context=".ui.meetings.MeetingEventFragment"
    tools:listitem="@layout/fragment_meeting_event" />
    

    and this is the detail layout:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="1dp"
    android:layout_marginBottom="1dp"
    android:background="@drawable/background_border_meetings">
        <TextView
        android:id="@+id/slot_type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:text="slot_type"
        />
    </RelativeLayout>
    

    background_border_meetings is just a box:

    <?xml version="1.0" encoding="utf-8"?>    
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
        <!-- This is the stroke you want to define -->
        <stroke android:width="4dp"
        android:color="@color/mdtp_line_dark"/>
        <!-- Optional, round your corners -->
        <corners android:bottomLeftRadius="2dp"
        android:topLeftRadius="2dp"
        android:bottomRightRadius="2dp"
        android:topRightRadius="2dp" />
        <gradient android:startColor="@android:color/transparent"
        android:endColor="@android:color/transparent"
        android:angle="90"/>
    </shape>
    
    0 讨论(0)
提交回复
热议问题