更改Textview的取消颜色

时间:2016-10-26 09:22:33

标签: android textview android-paint

我有7 TextView一次只能选择一个TextView,所选TextView应包含下划线,我只想更改下划线颜色(不是addUnderLineToText.setPaintFlags(addUnderLineToText.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); 颜色)。

我使用以下代码强调if ((removeUnderLine[i].getPaintFlags() & Paint.UNDERLINE_TEXT_FLAG) > 0) { textview[i].setPaintFlags(textview[i].getPaintFlags() & (~Paint.UNDERLINE_TEXT_FLAG)); }

{{1}}

我正在使用以下行删除。

{{1}}

1 个答案:

答案 0 :(得分:0)

首先,我为XML布局文件中的轻松自定义定义了自定义属性

<declare-styleable name="UnderlinedTextView" >
<attr name="underlineWidth" format="dimension" />
<attr name="underlineColor" format="color" />

自定义Textview类

public class UnderlinedTextView extends TextView {

private Rect lineBoundsRect;
private Paint underlinePaint;

public UnderlinedTextView(Context context) {
    this(context, null, 0);
}

public UnderlinedTextView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public UnderlinedTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs, defStyleAttr);
}

private void init(Context context, AttributeSet attributeSet, int defStyle) {

    float density = context.getResources().getDisplayMetrics().density;

    TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.UnderlinedTextView, defStyle, 0);
    int mColor = typedArray.getColor(R.styleable.UnderlinedTextView_underlineColor, 0xFFFF0000);
    float mStrokeWidth = typedArray.getDimension(R.styleable.UnderlinedTextView_underlineWidth, density * 2);
    typedArray.recycle();

    lineBoundsRect = new Rect();
    underlinePaint = new Paint();
    underlinePaint.setStyle(Paint.Style.STROKE);
    underlinePaint.setColor(mColor); //color of the underline
    underlinePaint.setStrokeWidth(mStrokeWidth);
}

@ColorInt
public int getUnderLineColor() {
    return underlinePaint.getColor();
}

public void setUnderLineColor(@ColorInt int mColor) {
    underlinePaint.setColor(mColor);
    invalidate();
}

public float getUnderlineWidth() {
   underlinePaint.getStrokeWidth()
}

public void setUnderlineWidth(float mStrokeWidth) {
    underlinePaint.setStrokeWidth(mStrokeWidth);
    invalidate();
}

@Override
protected void onDraw(Canvas canvas) {

    int count = getLineCount();

    final Layout layout = getLayout();
    float x_start, x_stop, x_diff;
    int firstCharInLine, lastCharInLine;

    for (int i = 0; i < count; i++) {
        int baseline = getLineBounds(i, lineBoundsRect);
        firstCharInLine = layout.getLineStart(i);
        lastCharInLine = layout.getLineEnd(i);

        x_start = layout.getPrimaryHorizontal(firstCharInLine);
        x_diff = layout.getPrimaryHorizontal(firstCharInLine + 1) - x_start;
        x_stop = layout.getPrimaryHorizontal(lastCharInLine - 1) + x_diff;

        canvas.drawLine(x_start, baseline + mStrokeWidth, x_stop, baseline + mStrokeWidth, underlinePaint);
    }

    super.onDraw(canvas);
 }
}

然后它的用法很简单

<some.package.UnderlinedTextView
android:id="@+id/tvTest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:gravity="center"
android:text="This is a demo text"
android:textSize="16sp"
app:underlineColor="#ffc112ef"
app:underlineWidth="3dp"/>