Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何设置scrollView的最大高度?_Android_Android Constraintlayout - Fatal编程技术网

Android 如何设置scrollView的最大高度?

Android 如何设置scrollView的最大高度?,android,android-constraintlayout,Android,Android Constraintlayout,如何设置scrollView的maxHeight属性? 我有一个LinearLayout,我以编程方式向这个布局添加了一些视图(行)。当行达到scrollView的最大高度时,如何显示滚动?ScrollView位于CardView中 <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent

如何设置scrollView的maxHeight属性? 我有一个
LinearLayout
,我以编程方式向这个布局添加了一些视图(行)。当行达到scrollView的最大高度时,如何显示滚动?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>

更改ScrolView父视图的高度。在您的情况下,CardView是父布局。因此,将CardView的高度设置为
android:layout\u height=“350dp”
或者在CardView的父级中使用线性布局,根据需要使用权重。如果在适配器中使用此布局或类似的布局,则仅使用数字设置高度


由于您使用的是
ConstraintLayout
,因此需要向
CardView

app:layout_constraintTop 

app:layout_constraintBotttom
您还需要添加以下行

app:layout_constrainedHeight="true"

app:layout_constraintHeight_max="300dp" // Change this to your maximum height
应该是这样的

<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
//根据您的布局进行更改

在滚动视图中使用此属性,并更改LinearLayout高度以匹配父项

android:fillViewport="true"
像这样

<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
<ScrollView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
        android:fillViewport="true"> 

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

    </ScrollView>


如果在布局中有一个项目两个项目,它会显示空白。请详细说明它显示空白的位置好吗?还重新发布新代码。。。我查一下是否有什么问题。我需要最大高度。示例:如果项目超过3个,则显示滚动条,否则显示包含换行内容的项目您是否尝试在线性布局中设置权重和?它隐藏了什么视图?在布局编辑器中还是在实际设备上?在布局编辑器中?您添加了什么约束条件?如果您在具有实际内容的设备上运行时看不到卡,请在我这样尝试时将
cardwiew
的高度更改为
0dp
:它似乎确实有效嗯。。。不工作。在我的第二张名片上可能有问题。cardView 2被约束在cardView下方