无法在 CardView 内滚动文本

问题描述 投票:0回答:1

我尝试使文本“@string/info1”可以在cardView内部滚动,但它不起作用。展开cardView后,它会显示所有文本,这使得cardView的尺寸变得如此之大。我该如何解决这个问题?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.infoApp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:orientation="vertical"
        android:gravity="center">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="@drawable/cardbackground">
    
                <androidx.cardview.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:maxHeight="200dp"
                    android:onClick="expand"
                    android:layout_margin="20dp"
                    app:cardCornerRadius="20dp"
                    app:cardElevation="10dp"
                    app:contentPadding="20dp">
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/card_layout1"
                        android:orientation="vertical"
                        android:animateLayoutChanges="true">
    
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="WHO"
                            android:textSize="30sp"
                            android:textStyle="bold"
                            android:textColor="#113B68"/>
    
                        <ScrollView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content">
    
                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:id="@+id/detail_info1"
                                android:visibility="gone"
                                android:text="@string/info1"
                                android:textSize="18sp"/>
    
                        </ScrollView>
    
                    </LinearLayout>
    
                    <ImageView
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:id="@+id/icon_layout1"
                        android:layout_gravity="end|right"
                        android:src="@drawable/baseline_arrow_drop_down_24" />
    
                </androidx.cardview.widget.CardView>
    
            </LinearLayout>
    
    </LinearLayout>

before expanding cardview

after expanding cardview

这里是展开cardView之前和之后的一些屏幕截图。

java android cardview
1个回答
0
投票

当高度在wrap_content中时,它会扩展视图本身,因此将cardView的高度更改为固定高度,这样它就会按照你的预期工作。

© www.soinside.com 2019 - 2024. All rights reserved.