android - android表格垂直列分隔符
问题描述
我正在尝试在 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>
解决方案
我发现在单个单元格上使用背景来实现分隔符更容易。
例如创建一个像下面这样的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"/>
其他带有矩形和矩形组合的可绘制对象可以为您提供完整的边框或任何边。
推荐阅读
- html - bootstrap4-datetimepicker 时间选择器定位
- java - Android SQLite:我无法选择整数字段
- c - 弗洛伊德三角反转
- r - 在 mutate 命令中使用 switch
- javascript - 在 React 中渲染之前对元素进行排序
- typescript - RealmJS TypeError:无法读取未定义的属性“领域”
- reporting-services - 将多个值传递给子报表参数 - SSRS
- c# - 如何从 arrayString 中选择特定的随机字符串?
- php - 如何使用 Microsoft Graph API 创建一个没有错误的文件夹?
- python-3.x - webdriver移动仿真python中的向下滑动功能