简体   繁体   English

布局边距不适用于 RecyclerView 中的约束布局

[英]Layout margin not applying to constraint layout in RecyclerView

I am attempting to add spacing between views in the RecyclerView.我正在尝试在 RecyclerView 中的视图之间添加间距。 Using the RecyclerView I have a repeating list of constraint views using a view adapter.使用 RecyclerView 我有一个使用视图适配器的约束视图的重复列表。 On each constraint view I have set the layout margin to equal 15dp like so android:layout_margin="15dp" .在每个约束视图上,我都将布局边距设置为等于 15dp,就像android:layout_margin="15dp" When previewing the layout in design view I can see that the margin is being applied however when the app is compiled the RecyclerView.在设计视图中预览布局时,我可以看到正在应用边距,但是当应用程序编译 RecyclerView 时。

The XML for the view being used inside the RecyclerView is as follows: 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>

In design view, the component I am designing looks like this:在设计视图中,我正在设计的组件如下所示:

设计视图

However, when compiled and being used in the RecyclerView, the margins are not applied:但是,在 RecyclerView 中编译和使用时,不应用边距:

编译

This is my code where I inflate the layout.这是我对布局进行膨胀的代码。 Originally the width of the view was not being applied so as shown in this answer I changed my code.最初未应用视图的宽度,因此如本答案所示,我更改了代码。

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);

Searched around quite extensively now and can't seem to find a solution.现在进行了相当广泛的搜索,似乎找不到解决方案。

Try to to this:尝试这样做:

<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">

Or what i always do:或者我经常做的:

<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">

I've managed to figure out the solution.我已经设法找出解决方案。 It turns out the answer that I followed to try and resolve the width issue was causing the margin issue.事实证明,我遵循的尝试解决宽度问题的答案导致了边距问题。 Instead of setting null as the ViewGroup parameter on the inflate function, I have now passed ViewGroup into the inflate function like so:我现在没有将 null 设置为 inflate 函数的 ViewGroup 参数,而是像这样将 ViewGroup 传递给 inflate 函数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM