使用包装内容时,android中最大高度的权重



我的布局中有两个回收器视图,我希望第二个rv的最大高度为40%,否则高度应为包装内容。

<LinearLayout> <!-- vertical -->
<androidx.recyclerview.widget.RecyclerView/> <!-- fill the height -->
<androidx.recyclerview.widget.RecyclerView/> <!-- wrap the height, max: 40% -->
</LinearLayout>

这是我想要实现的,但不知道如何实现?

这可以通过约束布局轻松实现。在约束布局中,Guideline视图用于实现%。

这是您的用例的一个例子

<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/guideline3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintGuide_percent="0.60"
app:layout_constraintStart_toStartOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/recyclerView"/>
</androidx.constraintlayout.widget.ConstraintLayout> 

在第一个回收器视图中,将占据屏幕的60%,第二个视图将自动占据屏幕的40%。

最新更新