给TextView添加行分割线

给TextView添加行分割线

思路:通过在TextView onDraw的方法里把行分割线画出来

自定义两个属性
<declare-styleable name="LineDividerTextView">
        <attr name="line_divider" format="reference"/>
        <attr name="line_divider_height" format="dimension"/>
</declare-styleable>

line_divider 分割线的drawable,可以是一个颜色,图片,只要是drawable就可以
line_divider_height 分割线的高度

画分割线

我们只需找出行与行中间的坐标即可

可以通过getLineBounds(int line, Rect bounds)这个方法获取行的边框。注意的地方是行的边框为文字加上行间距(设置了lineSpaceExtra),所以行与行中间的y坐标为
底部坐标减去 行间距的一半

onDraw代码如下

@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int count = getLineCount();

        for (int i = 0; i < count; i++) {
            // last line not draw
            if (i == count - 1) {
                break;
            }
            getLineBounds(i, mRect);

            lineDivider.setBounds(
                    mRect.left,
                    (int) (mRect.bottom - getLineSpacingExtra() / 2 - lineDividerHeight / 2),
                    mRect.right,
                    (int) (mRect.bottom - getLineSpacingExtra() / 2 + lineDividerHeight / 2));
            lineDivider.draw(canvas);
        }

    }
然后再xml引用这个自定义view
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="8dp">

        <etwge.yu.widget.LineDividerTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:lineSpacingExtra="8dp"
            android:text="@string/text1"
            android:textSize="16sp"
            app:line_divider="@drawable/divider_gradient"
            app:line_divider_height="1dp" />

        <etwge.yu.widget.LineDividerTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:lineSpacingExtra="8dp"
            android:text="@string/text2"
            android:textSize="16sp"
            app:line_divider="@drawable/divider_shadow"
            app:line_divider_height="6dp" />

        <etwge.yu.widget.LineDividerTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:lineSpacingExtra="8dp"
            android:text="@string/text3"
            android:textSize="16sp"
            app:line_divider="@drawable/divider"
            app:line_divider_height="6dp" />

    </LinearLayout>

如图
示例图片

需要注意的地方

根据line_divider_height 适当设置lineSpaceExtra的大小

行间距兼容问题

5.0以下lineSpaceExtra 会影响
到最后一行,最后一行也会有行间距,5.0以上则不会,如果需要解决这个问题,可以看我上一篇文章填填Android lineSpacingExtra 的坑

源码

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 TableLayout 中设置之间的分割线可以通过在布局文件中添加一个 View 控件来实现,然后设置该 View 的背景色为分割线颜色,高度为分割线宽度,宽度为 match_parent,这样就可以把整个分割线的效果实现出来。 具体操作步骤如下: 1. 在 TableLayout 中添加一个 View 控件,设置其高度为分割线的宽度,宽度为 match_parent。 2. 在 View 控件的背景色中设置分割线的颜色。 3. 在每个 TableRow 标签中都添加一个这样的 View 控件,以实现整个表格的分割线效果。 示例代码如下: ```xml <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TableRow> <TextView android:text="姓名" android:padding="5dp" /> <TextView android:text="年龄" android:padding="5dp" /> </TableRow> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/divider_color" /> <TableRow> <TextView android:text="张三" android:padding="5dp" /> <TextView android:text="18" android:padding="5dp" /> </TableRow> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/divider_color" /> <TableRow> <TextView android:text="李四" android:padding="5dp" /> <TextView android:text="20" android:padding="5dp" /> </TableRow> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/divider_color" /> </TableLayout> ``` 其中,@color/divider_color 是分割线的颜色,可以在 values 文件夹下的 colors.xml 文件中进定义。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值