【问题标题】:How to add margin only at start & end of each row in grid recyclerview如何仅在网格回收器视图中每行的开头和结尾添加边距
【发布时间】:2020-03-02 20:28:51
【问题描述】:

我为RecyclerView 创建了以下布局,并以网格样式显示它。

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:paddingBottom="24dp"
        android:background="@drawable/shadow_player_stats"
        xmlns:app="http://schemas.android.com/apk/res-auto">



    <TextView
            android:id="@+id/tvPlayerStats"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginTop="12dp"
            android:letterSpacing="0.1"
            android:maxLength="4"
            android:maxLines="1"
            android:text="-"
            android:includeFontPadding="false"
            android:textColor="@color/white"
            android:textSize="34dp"
            android:textStyle="bold"
            app:layout_constraintStart_toStartOf="@+id/ivGames"
            app:layout_constraintTop_toTopOf="@+id/ivGames" />

        <TextView
            android:id="@+id/tvLabel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="12dp"
            android:layout_marginEnd="12dp"
            android:maxLines="2"
            android:lines="2"
            android:text="DATA-TEST"
            android:textAllCaps="true"
            android:textColor="@color/white"
            android:textSize="14dp"
            android:textStyle="normal"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="@+id/ivGames"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="@+id/ivGames"
            app:layout_constraintTop_toBottomOf="@+id/tvPlayerStats"
            app:layout_constraintVertical_bias="0.0" />


    </androidx.constraintlayout.widget.ConstraintLayout>    

这正在生成以下类型的布局。

但我想在每行的开头和结尾添加间距,而不是在项目之间添加间距,如下所示

【问题讨论】:

  • 您可以将 paddingStart 和 paddingEnd 添加到 RecyclerView (不是添加到您在问题中膨胀和共享的布局......而是添加到 RecyclerView 本身)
  • Recyclerview 由 3 个布局组成,其中 1 个是线性线,如果我在 recyclerview 添加填充或边距,那么这些线不会连接到末尾。
  • @IvanWooll : 真的很有帮助,谢谢分享 :)

标签: android android-layout android-recyclerview android-constraintlayout android-gridlayout


【解决方案1】:

在回收站视图中添加 start/left 和 end/right padding

试试这个

  <androidx.recyclerview.widget.RecyclerView
                            android:id="@+id/draft_recylcerview"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" 
                            android:paddingLeft="@dimen/margin_10"
                            android:paddingRight="@dimen/margin_10"/>

【讨论】:

    【解决方案2】:

    试试下面的解决方案。

    这是我的自定义布局,包含 TextView my_custom_row.xml

    <?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="wrap_content"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/myCustomTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="8dp"
            android:paddingBottom="8dp"
            android:textColor="@android:color/holo_green_dark"
            android:textSize="16sp"
            android:textStyle="bold" />
    
    </LinearLayout>
    

    在上面的代码中你可以看到我在TextView中设置了paddingTop和paddingBottom,所以它会生成类似这样的布局

    现在,当我只想在 recyclerview 的开始和结束处设置更多边距/填充时,我必须在 recyclerview xml 代码中设置以下属性。

    android:clipToPadding="false"
    android:paddingTop="15dp"
    android:paddingBottom="15dp"
    

    会生成类似这样的布局

    注意:我减少了 textView 的填充

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-09
      • 1970-01-01
      • 2014-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多