Android 拉伸第二列以填满屏幕,但如果需要可以使用多行

Android stretch second column to fill the screen but with multiple lines if neeeded

我有一个包含 2 列的 table。第一列仅包含宽度固定的图像。第二列包含的文本可能很短或太长而无法放在一行中。

我希望 table 完全填满屏幕但不超过它。如果需要,文本可以分成多行。此外,两列都应该有一些填充。

有人建议我对第一列使用权重 0,对第二列使用权重 1。

我的table第一行是:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/rlMapDetail"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:background="@color/white"
>

<TableRow>
    <ImageView
        android:id="@+id/imgAddress"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/map_address_detail"
        android:layout_weight="0"
        android:padding="5dp"
        />
    <TextView
        android:id="@+id/txtAddress"
        android:textColor="@color/black_gray"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|left"
        android:singleLine="false"
        android:inputType="textMultiLine"
        android:layout_weight="1"
        android:padding="5dp"
        android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
        />
</TableRow>

TableLayout 的总宽度超过了屏幕尺寸,因此我无法处理 table 的后续行。

我终于找到解决这个问题的方法是添加:

         android:stretchColumns="1"
         android:shrinkColumns="1"

TableLayout

我认为您不应该提供权重,因为 imageview 的宽度是固定的。您需要将宽度匹配父级设置为 textview。