You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中CardView内文本无法滚动,展开后视图过大问题求助

解决CardView展开后文本撑大布局且无法滚动的问题

问题说明

尝试让CardView内的@string/info1文本实现滚动效果但失败,展开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>

解决方法

核心问题是ScrollView使用wrap_content作为高度时,会完全包裹内部文本,失去滚动特性,同时CardView的maxHeight因内部布局无高度限制而失效。按以下步骤修改:

  • 限制ScrollView的高度
    将ScrollView的android:layout_height="wrap_content"改为固定高度或最大高度,同时确保内容超过该高度时可滚动:

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:maxHeight="150dp" <!-- 可根据需求调整此值 -->
        android:fillViewport="true">
    
    • android:maxHeight:限制ScrollView的最大高度,避免撑大CardView
    • android:fillViewport="true":确保ScrollView在内容不足时也占满设置的高度,布局更整齐
  • 调整CardView的高度约束
    保留CardView的android:maxHeight="200dp",同时确保内部LinearLayout的高度不会超出该值。由于标题TextView占用了部分高度,ScrollView的maxHeight需与CardView的maxHeight配合(比如CardView设200dp,ScrollView设150dp,留出标题和padding的空间)。

  • 优化展开逻辑
    在expand点击方法中,切换detail_info1的可见性时,无需调整布局高度,因为ScrollView已被限制高度:

    public void expand(View view) {
        TextView detailInfo = findViewById(R.id.detail_info1);
        ImageView arrowIcon = findViewById(R.id.icon_layout1);
        if (detailInfo.getVisibility() == View.GONE) {
            detailInfo.setVisibility(View.VISIBLE);
            arrowIcon.setImageResource(R.drawable.baseline_arrow_drop_up_24);
        } else {
            detailInfo.setVisibility(View.GONE);
            arrowIcon.setImageResource(R.drawable.baseline_arrow_drop_down_24);
        }
    }
    

修改后,展开CardView时文本会在ScrollView的限制高度内滚动,不会撑大整个CardView。


内容的提问来源于stack exchange,提问作者fiTzz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 00:43:14