首页 > 解决方案 > 当文本溢出而不是换行时,TextView 会拉伸 CardView

问题描述

我有 2 个 MaterialCardViews,我想在屏幕上共享相同的宽度,并使用具有 2 列的 GridLayout 实现了这一点,但是当 MaterialCardViews 中的文本开始长于所需时,我很难保持宽度约束宽度。

所需: https ://imgur.com/a/BB9H4zZ

当前问题: https ://imgur.com/a/gyXJxZc

这是我当前的卡片实现:

<GridLayout
                android:id="@+id/search_results_grid"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:columnCount="2"
                android:useDefaultMargins="true">

                <com.google.android.material.card.MaterialCardView
                    android:layout_columnWeight="1">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="#eaeaea"
                        android:lines="1"
                        android:text="String"
                        android:inputType="textMultiLine"
                        android:autoSizeTextType="uniform">
                    </TextView>
                </com.google.android.material.card.MaterialCardView>


                <com.google.android.material.card.MaterialCardView
                    android:layout_columnWeight="1">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:text="String"
                        android:background="#eaeaea">
                    </TextView>
              </com.google.android.material.card.MaterialCardView>


            </GridLayout>

从其他 stackoverflow 帖子中,我尝试添加android:inputType="textMultiline"、禁用水平滚动,android:autoSizeTextType="uniform"但没有产生任何更改。

标签: javaandroidxml

解决方案


使用LinearLayout而不是GridLayout显示您的CardViews,并赋予CardViews以下两者同等的权重:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/search_results_grid"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.material.card.MaterialCardView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:autoSizeTextType="uniform"
            android:background="#eaeaea"
            android:inputType="textMultiLine"
            android:lines="1"
            android:text="StringLongLongStringStringStringStringString"/>
    </com.google.android.material.card.MaterialCardView>


    <com.google.android.material.card.MaterialCardView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:autoSizeTextType="uniform"
            android:background="#eaeaea"
            android:inputType="textMultiLine"
            android:lines="1"
            android:text="StringLongLongString"/>
    </com.google.android.material.card.MaterialCardView>


</LinearLayout>

如果您android:lines从 中删除标签TextView,则下一个内容将进入下一行,而不是像现在发生的那样转发。


推荐阅读