个性化strikeThrough Textview

时间:2012-08-07 12:41:29

标签: android textview text-size

我使用它来通过textView

进行调整
  

textView.setPaintFlags(textView.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);

但现在我想个性化颜色并将其旋转45°以适应内容的对角线。

我绑的是什么:

我虽然关于扩展textview小部件并覆盖onDraw方法..但我在设计时非常初学......

所以有什么帮助如何在textView上绘制红色(彩色)旋转线?

3 个答案:

答案 0 :(得分:2)

  

加入Mayani的回复

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

答案 1 :(得分:0)

为此,请按照以下步骤操作:

  1. 通过扩展View
  2. 创建自定义TextView
  3. 在XML布局中声明此自定义textview,就像我们对TextView,Button小部件一样。
  4. 例如:

      class CustomTextView extends View {
            private int mColor;
            private int mRotationAngle, mRotationW, mRotationH;
    
                private String mText;
             public CustomTextView(Context context) { 
                super(context);
                // set default parameters
                mColor = Color.WHITE;
                mRotationAngle = 0;
             }
    
             public void SetColor(int newcolor) {
                mColor = newcolor;
                this.invalidate();
             } 
    
           public void SetRotation(int newangle, int neww, int newh) {
            mRotationAngle = newangle;
            mRotationW = neww;
            mRotationH = newh;
            this.invalidate();
        }
    
           public void SetText(String newtext) {
                mText = newtext;
                this.invalidate();
            }
    
        @Override 
            protected void onDraw(Canvas canvas) { 
                Paint paint = new Paint(); 
                paint.setStyle(Paint.Style.FILL); 
                canvas.rotate(mRotationAngle,mRotationW,mRotationH);
                canvas.drawText(mText, 0, 0, paint);
                super.onDraw(canvas); 
            } 
        }
    

    <强>更新

    检查Specifying “strikethrough” on a section of TextView text

答案 2 :(得分:0)

使用以下方法,您可以实现此功能,而无需在Java代码中弄乱UI:

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:layout_centerVertical="true">

                    <TextView
                        android:id="@+id/textview1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentStart="true"
                        android:text="$99"
                        android:textSize="12sp" />

                    <View
                        android:layout_width="0dp"
                        android:layout_height="1dp"
                        android:layout_centerVertical="true"
                        android:layout_alignLeft="@+id/textview1"
                        android:layout_alignRight="@+id/textview1"
                        android:background="@color/fav_dark_red_color" />
                </RelativeLayout>