2

これを使用して、textViewをストライクします

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

しかし今、私は色をパーソナライズして、コンテンツの対角線に合うように45°回転させたいと思います。

私が結んだもの:

textviewウィジェットを拡張してonDrawメソッドをオーバーライドすることについては考えましたが..しかし、私は設計の初心者です...

では、textViewに赤(色)の回転した線を描画する方法はありますか?

4

3 に答える 3

2

マヤニの応答に追加

@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);
}
于 2012-08-07T15:23:25.057 に答える
0

そのためには、以下の手順に従ってください。

  1. クラスを拡張してカスタムTextViewを作成するView
  2. TextView、Buttonウィジェットの場合と同じように、XMLレイアウト内でこのカスタムtextviewを宣言します。

例えば:

  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); 
        } 
    }

アップデート:

TextViewテキストのセクションで「取り消し線」を指定しているかどうかを確認します

于 2012-08-07T12:46:37.723 に答える
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>
于 2016-12-20T06:17:04.227 に答える