android表格垂直列分隔符

时间:2021-05-25 09:07:29

标签: android android-tablelayout divider

我正在尝试在 android 表格布局中的列之间添加垂直分隔线,并尝试了多种解决方案,但没有成功。这是我的表格代码结果的图像

由于某种原因,即使提到了宽度,视图也会占用一整列来显示分隔线

表格代码

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="20dp">

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:text="@string/item_code"
            android:textStyle="bold" />

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/red_" />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:text="@string/item_desc"
            android:textStyle="bold" />

    </TableRow>

    <View
        android:layout_height="2dip"
        android:background="@color/red_" />

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:text="SFT001" />

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/red_" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:text="Full table" />

    </TableRow>

</TableLayout>

table with divider added

1 个答案:

答案 0 :(得分:0)

我发现在单个单元格上使用背景来实现分隔线更容易。

例如创建一个类似于下面的 drawable 叫做 right_border.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/red" />
        </shape>
    </item>
    <item android:right="3dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/white" />
        </shape>
    </item>
</layer-list>

(这是一个红色矩形,顶部有一个白色矩形,但留下 3dp 的红色暴露)

然后在单元格(TextView)上

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:text="@string/item_code"
            android:textStyle="bold" 
            android:background="@drawable/right_border"/>

其他带有矩形和矩形组合的可绘制对象可以为您提供完整的边框或任何边。