![](/img/trans.png)
[英]Any way to set barrier at a distance constraint layout or adding margin to a view the side with no constraint?
[英]Layout margin not applying to constraint layout in RecyclerView
我正在嘗試在 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 中編譯和使用時,不應用邊距:
這是我對布局進行膨脹的代碼。 最初未應用視圖的寬度,因此如本答案所示,我更改了代碼。
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);
現在進行了相當廣泛的搜索,似乎找不到解決方案。
嘗試這樣做:
<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">
我已經設法找出解決方案。 事實證明,我遵循的嘗試解決寬度問題的答案導致了邊距問題。 我現在沒有將 null 設置為 inflate 函數的 ViewGroup 參數,而是像這樣將 ViewGroup 傳遞給 inflate 函數:
View view = inflater.inflate(R.layout.text_moment, viewGroup, false);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.