如何使用特定颜色在android中删除textview

时间:2015-11-23 06:18:40

标签: android

我一直在尝试将textview删除线颜色更改为RED,但它给了我与textview相同的颜色。 我试过的代码是

Paint paint=new Paint();
paint.setColor(Color.RED);
textview.setPaintFlags(holder.product_cost.getPaintFlags() | paint.STRIKE_THRU_TEXT_FLAG);

请帮我解决这个问题。

Iam getting this

2 个答案:

答案 0 :(得分:1)

  

使用TextView方法在整个Ondraw中绘制线条。

    public class CustomTextView extends TextView {
        private int mColor;
        private Paint paint;

        public CustomTextView (Context context) {
            super(context);
            init(context);
        }

        public CustomTextView (Context context, AttributeSet attrs) {
            super(context, attrs);
            init(context);
        }

        public CustomTextView (Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init(context);
        }

        private void init(Context context) {
            Resources resources = context.getResources();
            //Color
            mColor = resources.getColor(R.color.blue);

            paint = new Paint();
            paint.setColor(mColor);
            //Width
            paint.setStrokeWidth(5);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawLine(0, 25, getWidth(), 25, paint);
        }
    }

<强>用法

<package.CustomTextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Strike Me"
        android:textSize="50sp"/>

您可以自定义打击的位置,如果需要使用所应用的TextView颜色进行打击,可以执行以下操作。

使用资源文件

<resource>
    <string id="@+id/strike_one"><strike>Strike Me!</strike></string>
</resources>

<强>编程

TextView text= (TextView) findViewById(R.id.some_label);
text.setText("Strike Me!");
text.setPaintFlags(text.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

答案 1 :(得分:0)

你可以使用onDraw方法

@Override
   protected void onDraw(Canvas canvas) {
    Paint paint = new Paint();
    paint.setColor(strikeThroughColor);
    paint.setStyle(Paint.Style.FILL); 
    paint.setStrikeThruText(true);
    paint.setStrokeWidth(strikeThroughWidth);
    paint.setFlags(Paint.ANTI_ALIAS_FLAG);
    super.onDraw(canvas);
    float width = getWidth();
    float heigh = getHeight();
    canvas.drawLine(width/10, heigh/10, (width-width/10),(heigh-heigh/10), paint);
} 

参考此链接:How to change color of strikethrough