【问题标题】:Layout margin not applying to constraint layout in RecyclerView布局边距不适用于 RecyclerView 中的约束布局
【发布时间】:2021-06-09 22:10:13
【问题描述】:

我正在尝试在 RecyclerView 中添加视图之间的间距。使用 RecyclerView 我有一个使用视图适配器的重复约束视图列表。在每个约束视图上,我都将布局边距设置为等于 15dp,就像 android:layout_margin="15dp" 一样。在设计视图中预览布局时,我可以看到正在应用边距,但是当应用程序编译 RecyclerView 时。

RecyclerView 中使用的视图的 XML 如下:

<android.support.constraint.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="wrap_content"
android:layout_margin="15dp"
android:background="@drawable/gradient"
android:clipToPadding="true"
android:minHeight="0dp">

<TextView
    android:id="@+id/moment_timestamp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="20dp"
    android:layout_marginTop="16dp"
    android:text="28 MAR @ 2:45pm"
    android:textColor="@android:color/white"
    app:layout_constraintEnd_toStartOf="@+id/moment_text"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/moment_text"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    android:layout_marginEnd="20dp"
    android:layout_marginStart="20dp"
    android:layout_marginTop="40dp"
    android:background="@android:color/transparent"
    android:includeFontPadding="false"
    android:paddingVertical="0dp"
    android:text="A kind man held the door  open for me today"
    android:textAlignment="viewStart"
    android:textColor="@android:color/white"
    android:textSize="25sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/moment_timestamp" />

</android.support.constraint.ConstraintLayout>

在设计视图中,我正在设计的组件如下所示:

但是,在 RecyclerView 中编译和使用时,不应用边距:

这是我膨胀布局的代码。最初没有应用视图的宽度,如this answer 所示,我更改了代码。

View view = inflater.inflate(R.layout.text_moment, null, false);
RecyclerView.LayoutParams lp = new 
RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
return new MomentViewHolder(view);

现在搜索了很多,似乎找不到解决方案。

【问题讨论】:

标签: java android android-recyclerview android-constraintlayout


【解决方案1】:

试试这个:

<android.support.constraint.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="wrap_content"
android:layout_marginBottom="15dp" //This line is the important one
android:background="@drawable/gradient"
android:clipToPadding="true"
android:minHeight="0dp">

或者我一直在做的事情:

<android.support.constraint.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="wrap_content"
android:padding="16dp" //This line is the important one
android:background="@drawable/gradient"
android:clipToPadding="true"
android:minHeight="0dp">

【讨论】:

  • 见上文,只是设法弄明白了 :) 感谢您抽出宝贵的时间。
  • 很好,快乐的编码。 =)
【解决方案2】:

我已经设法找出解决方案。事实证明,我尝试解决宽度问题的答案是导致边距问题。我现在没有将 null 设置为 inflate 函数的 ViewGroup 参数,而是将 ViewGroup 传递给 inflate 函数,如下所示:

View view = inflater.inflate(R.layout.text_moment, viewGroup, false);

【讨论】:

  • 您应该将其标记为重复。因为在未定义父级时有很多关于忽略参数的问题
猜你喜欢
  • 1970-01-01
  • 2018-09-03
  • 2020-02-05
  • 1970-01-01
  • 1970-01-01
  • 2017-12-09
  • 1970-01-01
  • 2018-06-01
  • 2019-01-05
相关资源
最近更新 更多