带删除线的TextView,删除线颜色与宽度可以设置
2018-02-05 15:58
218 查看
一.定义三个属性:删除线的颜色,宽度,是否显示删除线
二.自定义view继承自TextView
三.XML中使用
四.Activity中使用
<declare-styleable name="DeleteLineTextView"> <attr name="showDeleteLine"> <enum name="show" value="0" /> <enum name="hide" value="1" /> </attr> <attr name="deleteLineColor" format="color|reference" /> <attr name="deleteLineWidth" format="dimension" /> </declare-styleable>
二.自定义view继承自TextView
import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.text.TextPaint; import android.util.AttributeSet; import android.util.Log; import android.widget.TextView; /** * 带删除线的TextView,删除线颜色与宽度可以设置 */ public class DeleteLineTextView extends TextView { public static final String TAG = "DeleteLineTextView"; Paint linePaint; int showDeleteLine; int deleteLineColor; int deleteLineWidth; int i; public static final int SHOW = 0; public static final int HIDE = 1; public DeleteLineTextView(Context context) { this(context, null); } public DeleteLineTextView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public DeleteLineTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DeleteLineTextView); int indexCount = typedArray.getIndexCount(); showDeleteLine = typedArray.getInt(R.styleable.DeleteLineTextView_showDeleteLine, HIDE); deleteLineColor = typedArray.getColor(R.styleable.DeleteLineTextView_deleteLineColor, Color.BLACK); deleteLineWidth = (int) typedArray.getDimension(R.styleable.DeleteLineTextView_deleteLineWidth, 1f); typedArray.recycle(); Log.e(TAG, "deleteLineWidth:" + deleteLineWidth); i = dip2px(context, deleteLineWidth); linePaint = new Paint(); } @Override protected void onDraw(Canvas canvas) { if (showDeleteLine == SHOW) { linePaint.setAntiAlias(true); linePaint.setColor(deleteLineColor); linePaint.setStrokeWidth(i); int lineCount = this.getLineCount(); if (lineCount != 0) { int lineHeight = this.getLineHeight(); int baseline = this.getBaseline(); int viewWidth = getWidth(); int paddingLeft = getPaddingLeft(); int paddingRight = getPaddingRight(); TextPaint paint = getPaint(); String text = (String) getText(); int textLength = text.length(); Rect rect = new Rect(); paint.getTextBounds(text, 0, textLength, rect); int singleLineHeight = rect.height(); float textWidth = rect.width(); int halfHeight = baseline - singleLineHeight / 2; if (lineCount == 1) { canvas.drawLine(paddingLeft, halfHeight, textWidth + paddingLeft, halfHeight, linePaint); } else { //多行时,计算最后一行字符串所占长度 float sigleTextWidth = textWidth / textLength;//一个字符的宽度 int textSumInLine = (int) ((viewWidth - paddingLeft - paddingRight) / sigleTextWidth);//每行中字符的个数,int向下取余 int sigleLineTextWidth = (int) (textSumInLine * sigleTextWidth); int lastLineTextCount = textLength - textSumInLine * (lineCount - 1); int lastLineWidth = (int) (lastLineTextCount * sigleTextWidth); for (int i = 0; i < lineCount; i++) { if (i == lineCount - 1) { canvas.drawLine(paddingLeft, halfHeight + i * lineHeight, lastLineWidth + paddingLeft, halfHeight + i * lineHeight, linePaint); } else { canvas.drawLine(paddingLeft, halfHeight + i * lineHeight, sigleLineTextWidth + paddingLeft, halfHeight + i * lineHeight, linePaint); } } } } } super.onDraw(canvas); } public void setShowDeleteLine(boolean show) { if (show) { this.showDeleteLine = SHOW; } else { this.showDeleteLine = HIDE; } } public void setDeleteLineColor(int deleteLineColor) { this.deleteLineColor = deleteLineColor; } public void setDeleteLineWidth(Context context,int deleteLineWidth) { i = dip2px(context, deleteLineWidth); } public static int dip2px(Context context, float dpValue) { float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } }
三.XML中使用
xmlns:app="http://schemas.android.com/apk/res-auto"
<com.ycq.rongtest.DeleteLineTextView android:id="@+id/linetextview" android:layout_width="100dp" android:layout_height="wrap_content" android:background="#00ff00" android:paddingLeft="10dp" android:paddingRight="15dp" android:paddingTop="20dp" android:text="12345678900987876123456789009876" android:textSize="21sp" app:deleteLineWidth="1dp" app:deleteLineColor="#00ffff" app:showDeleteLine="show"/>
四.Activity中使用
DeleteLineTextView linetextview = (DeleteLineTextView) findViewById(R.id.linetextview); linetextview.setDeleteLineColor(Color.parseColor("#0000ff")); linetextview.setShowDeleteLine(true); linetextview.setDeleteLineWidth(this,1);
相关文章推荐
- SpannableStringBuilder设置TextView文字格式(颜色、背景色、下划线、删除线等)
- Edittext设置光标的颜色以及Textview下划线,中间删除线
- android TextView设置删除线,超链接,颜色和字体等说介绍
- android textview可以设置文字颜色 部分文字点击事件处理
- android TextView设置删除线,超链接,颜色和字体等说介绍
- 设置TextView中的部分位置可以点击,颜色不同
- android TextView设置删除线,超链接,颜色和字体等说介绍
- android TextView设置删除线,超链接,颜色和字体等说介绍,androidtextview
- 16-0 b879 1-13 TextView 设置文本颜色
- TextView(怎么用代码动态设置字体颜色)
- android--设置TextView部分文字的颜色和背景(高亮显示)
- ANDROID TEXTVIEW 设置字体颜色
- Android用代码给TextView中间部分设置不同的颜色
- 65、TextView 字体设置不同颜色 --- 未完
- TextView设置部分文字背景色和文字颜色
- android设置toolbar的menu itemTextView颜色
- Android中设置TextView的颜色setTextColor
- iOS-自定义TextView的方法,可以设置占位文字(placeholder)又可滚动
- textView中设置多种颜色的文字
- 安卓开发用Html.fromHtml给TextView设置文字不同颜色、大小