如何为 Table 行内的 Table 列设置背景颜色

How to set Background colour for the Table column inside the TableRow

我有 TableRow。在 table 行内,我有一些文本视图。

根据我的要求,Textview 高度应为 wrap_content。

如果我给 Background 颜色 它将被设置到 Textview。它不会填满整行。

就像下图一样

但我需要的是背景应该设置为整个列而不是文本视图。

这里我添加了我的代码。

<TableRow
        android:id="@+id/tableRow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:gravity="center"
        android:minHeight="?attr/actionBarSize">


        <TextView
            android:id="@+id/subject"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="198"
            android:background="@color/colorAccent"
            android:gravity="center"
            android:text="Subject"
            android:textColor="@color/colorWhite"
            android:textStyle="bold"></TextView>


        <TextView
            android:id="@+id/date"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="98"
            android:background="@color/colorSecondary"
            android:gravity="center"
            android:text="Date"
            android:textColor="@color/colorWhite"
            android:textStyle="bold"></TextView>

        <TextView
            android:id="@+id/Time"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="98"
            android:background="@color/colorPrimaryDark"
            android:gravity="center"
            android:text="Time"
            android:textColor="@color/colorWhite"
            android:textStyle="bold"></TextView>

    </TableRow>

只需将所有TextView中的layout_height改为match_parent即可。

如果 Textview android:layout_height="wrap_content" 是您的要求,请执行此操作在表格行中采用线性或相对位置背景颜色。在此布局中放置 textview。

由于在这种情况下您不希望高度被父级包裹,因此您需要创建三个线性布局并将其背景定义为 textView 的背景色。但是,如果您可以分享您需要它的原因,而不是建议更好的解决方案,那将是一笔不小的开销。