java - 当文本溢出而不是换行时,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"
但没有产生任何更改。
解决方案
使用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
,则下一个内容将进入下一行,而不是像现在发生的那样转发。
推荐阅读
- sql - REGEXP_SUBSTR 将逗号分隔的值拆分为不起作用的行
- java - Selenium,Java - 无法定位元素
- javascript - 在js中,为什么我的文件阅读器在我编辑它时无法跟踪我的输入文件
- mysql - 从多个表中提取数据(mysql)
- typescript - 在初始化期间返回给 vuex 插件的 vuex 商店的正确类型是什么
- crystal-lang - 使用 HTTP::Client.get("https...") 关闭流(IO::Error)
- jquery - 翻译 jquery 表
- javascript - 如何让引导 datetimepicker 只选择时间
- git - git cherry-pick 奇怪的问题,有时会发生冲突,但有时不会
- loops - 真的很基础的东西!使用 for 循环或 while 函数设计选择最少礼物数量的计划