赞
踩
我们有时候需要一个类似这样的显示,上面是文字,下面是一条线
这样的显示效果是TextView实现不了的,需要我们自己进行修改一下。
创建一个UnderlineTextView,继承系统的TextView
class UnderlineTextView(mContext: Context, attributeSet: AttributeSet) : androidx.appcompat.widget.AppCompatTextView(mContext, attributeSet) { private var underLinePains: Paint? = null init { underLinePains = Paint() underLinePains?.color = mContext.resources.getColor(R.color.agree_user_agreement_bottom_line_bg) underLinePains?.strokeWidth = 10f underLinePains?.strokeCap = Paint.Cap.SQUARE } override fun onDraw(canvas: Canvas?) { //注意这个顺序的问题,这样的顺序是先画TextView下面的带颜色的线段 underLinePains?.let { canvas?.drawLine( (0 - paddingStart).toFloat(), (height - 8).toFloat(), (width + paddingEnd).toFloat(), (height - 8).toFloat(), it ) } //现在是进行TextView里面的文本的绘制 super.onDraw(canvas) } }
这是一个很简单的View,实现了我们需要的功能,同样的当我们需要对TextView设置其他的要求时,也可以按照这种步骤实现。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。