如何设置滚动视图的最大高度?

Ben*_*tor 3 android android-constraintlayout

如何为 scrollView 设置 maxHeight 属性?我有一个LinearLayout,我以编程方式向这个布局添加了一些视图(行)。当行达到 scrollView 的 maxHeight 时,如何显示滚动?ScrollView 位于 CardView 中

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textViewTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constrainedHeight="true"
        app:layout_constraintBottom_toTopOf="@+id/cardView2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_max="100dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textViewTitle">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/linear"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" />
        </ScrollView>


    </androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

Gil*_*eig 12

因为您正在使用,所以ConstraintLayout您需要向您的CardView

app:layout_constraintTop 

app:layout_constraintBotttom
Run Code Online (Sandbox Code Playgroud)

你还需要添加以下几行

app:layout_constrainedHeight="true"

app:layout_constraintHeight_max="300dp" // Change this to your maximum height
Run Code Online (Sandbox Code Playgroud)

它应该看起来像这样

<androidx.cardview.widget.CardView 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    app:cardCornerRadius="@dimen/card_radius_10"
    app:layout_constrainedHeight="true"
    app:layout_constraintHeight_max="300dp" // Change this to your maximum height
    app:layout_constraintEnd_toEndOf="parent" 
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="parent" //Change based on your layout
    app:layout_constraintTop_toTopOf="parent"> //Change based on your layout
Run Code Online (Sandbox Code Playgroud)