ConstraintLayout链式布局致TextView宽度为0及RecyclerView高度异常
Hey there! Let's work through your two ConstraintLayout issues step by step. I've looked at your layout code and problem descriptions, so here's what's going on and how to fix it:
The root cause here is that your horizontal chain doesn't have weight set, and when you hide one TextView (set to GONE), ConstraintLayout can't properly calculate the width of the remaining one. Here's how to fix it:
- Add
app:layout_constraintHorizontal_weight="1"to bothtvElementandtvSensor. This ensures they split available horizontal space evenly when both are visible, and the remaining one takes up all available space when the other is hidden. - Set the chain style on the first element of the chain (
tvElement) to clarify layout behavior: addapp:layout_constraintHorizontal_chainStyle="spread". This keeps the two views spaced evenly with margins when both are present. - You can remove
app:layout_constraintHorizontal_bias="0.5"fromtvElement—the spread chain already centers the views automatically.
Adjusted code for the two TextViews:
<TextView android:id="@+id/tvElement" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:background="@drawable/shape_back" android:gravity="center" android:padding="10dp" android:text="@string/elements" android:textColor="@color/item_view_color" app:layout_constraintEnd_toStartOf="@+id/tvSensor" app:layout_constraintHorizontal_chainStyle="spread" app:layout_constraintHorizontal_weight="1" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/tvSensor" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:background="@drawable/shape_back" android:gravity="center" android:padding="10dp" android:text="@string/sensors" android:textColor="@color/item_view_color" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_weight="1" app:layout_constraintStart_toEndOf="@+id/tvElement" app:layout_constraintTop_toTopOf="parent" />
Now when you set one TextView to GONE, the other will automatically expand to fill available horizontal space (respecting margins) instead of collapsing to 0 width.
The issue here is a conflicting constraint on your Guideline that breaks the RecyclerView's height calculation:
Your Guideline has both app:layout_constraintGuide_begin="62dp" (fixes it 62dp from the top) and app:layout_constraintBottom_toTopOf="@+id/rv_channel" (tries to pin it to the RecyclerView's top). This creates a circular dependency—the RecyclerView's top depends on the Guideline, and the Guideline's bottom depends on the RecyclerView. ConstraintLayout can't resolve this correctly, so it shrinks the RecyclerView to its minimum content height.
Here's the fix:
- Remove
app:layout_constraintBottom_toTopOf="@+id/rv_channel"from the Guideline. You only needapp:layout_constraintGuide_begin="62dp"to fix its position 62dp below the top. - Keep the RecyclerView's
layout_height="0dp"(match_constraints) so it fills the space between the Guideline and the OK button. Your existingapp:layout_constraintHeight_min="250dp"works great to ensure it never gets too small.
Adjusted Guideline code:
<android.support.constraint.Guideline android:id="@+id/guideline" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintGuide_begin="62dp" app:layout_constraintTop_toTopOf="parent" />
Now when you set the RecyclerView's height to 0dp, it will correctly fill the remaining space between the Guideline and the OK button, while respecting your set minimum height. Using match_parent worked before because it ignored the conflicting constraints, but this is the proper ConstraintLayout approach.
内容的提问来源于stack exchange,提问作者noobEinstien

