每个App都会用到分割线,一个两边顶到头的分割线只需要使用配合background属性就能够轻松实现。
常见的分割线形式还有如下这种两端或者一端留出一定空白距离的分割线。

实现这种效果有很多解决方案,我能想到的有三种:
第一种,也是最简单的一种实现方式是:
android:layout_width="match_parent"
android:layout_height="@dimen/divider_height"
android:background="@android:color/white"
android:paddingLeft="@dimen/divider_padding"
android:paddingRight="@dimen/divider_padding">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"/>
这种实现方式会引起不必要的overdraw,因为容器的背景绘制了一次,然后分割线再绘制了一次。
第二种,可以用三个不同background的来避免overdraw
android:layout_width="match_parent"
android:layout_height="@dimen/divider_height"
android:orientation="horizontal">

本文介绍了在Android中实现自定义分割线的三种方法,包括使用背景属性、三层背景布局和自定义View绘制。着重讨论了自定义View的onDraw方法,通过canvas.drawLine绘制水平线,并调整坐标和线宽来实现两端留白的效果,避免overdraw并减少布局嵌套。
最低0.47元/天 解锁文章
993

被折叠的 条评论
为什么被折叠?



