我有 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"
但没有结果发生了变化。
最佳答案
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/search_results_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.card.MaterialCardView
android:id="@+id/leftCardView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/rightCardView">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#eaeaea"
android:lines="1"
android:text="StringLongLongStringStringStringStringString"/>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="@+id/rightCardView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@+id/leftCardView"
app:layout_constraintRight_toRightOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#eaeaea"
android:text="String"/>
</com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.widget.ConstraintLayout>
关于java - 当文本溢出而不是换行时,TextView 会拉伸(stretch) CardView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56509662/