使用约束布局
使用约束布局,版本2.0+
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
布局中
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/view_constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rc_filter"
android:layout_width="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_height="0dp"
app:layout_constraintHeight_default="wrap"
app:layout_constraintHeight_max="350dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
关键代码就是
android:layout_height="0dp"
app:layout_constraintHeight_default="wrap"
app:layout_constraintHeight_max="350dp"
当然也可以使用 ConstraintSet 在代码中设置来实现以上代码相同的结果:
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(view_constraintLayout);
constraintSet.constrainMaxHeight(R.id.rc_filter,Utils.dpToPx(mContext,350));
TransitionManager.beginDelayedTransition(view_constraintLayout);
constraintSet.applyTo(view_constraintLayout);
网友评论