You can achieve this using only XML as long as your parent layout is ConstraintLayout
, which yours seems to be. Make these changes to your RecyclerView
tag:
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHeight_default="wrap"
app:layout_constraintHeight_max="280dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/line/>
The key attributes are these three:
android:layout_height="0dp"
app:layout_constraintHeight_default="wrap"
app:layout_constraintHeight_max="280dp"
Normally you only use a 0dp
dimension when you constrain both sides of the view, but really all you have to do is provide enough constraints to define the view size (and constraining both sides is just the easiest way to do that). Once you have the height set to "match constraints", you can combine a default height constraint with a maximum height constraint to get exactly what you're looking for.
Note you must be using a 1.1 version of the constraint layout library for this. Make sure your app's build.gradle file has something like:
implementation 'com.android.support.constraint:constraint-layout:1.1.0-beta5'
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…