本文主要是介绍Android TextView 划线,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
要求:1.TextView 自定义划线功能,要求划线颜色为红字(可自定义)
2.TextView 部分内容划线,按照开始位置与结束位置划线
效果如下
public class RedTextView extends TextView {public RedTextView(Context context) {super(context);}public RedTextView(Context context, @Nullable AttributeSet attrs) {super(context, attrs);}public RedTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);if (show) {Rect satrtRect = new Rect();int start = getLayout().getLineForOffset(startPosition);getLayout().getLineBounds(start, satrtRect);float xAxisLeft = getLayout().getPrimaryHorizontal(startPosition);///字符左边x坐标float xAxisRight = getLayout().getSecondaryHorizontal(endPosition);//字符右边x坐标canvas.drawLine(xAxisLeft, satrtRect.centerY(), xAxisRight, satrtRect.centerY(), linePaint);}}Paint linePaint;int startPosition;int endPosition;boolean show = false;public void setShowText(String text, int startPosition, int endPosition) {this.setText(text);this.show = true;this.startPosition = startPosition;this.endPosition = endPosition;if (linePaint == null) {linePaint = new Paint();linePaint.setAntiAlias(true);linePaint.setColor(Color.RED);}invalidate();}public void setNoText(String text) {this.show = false;this.setText(text);}
}
这篇关于Android TextView 划线的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!