37

「Stackoverflow」が書かれた通常のTextViewがあるとしましょう.TextViewを-90°回転させて、Sを画面の下部に、Wを画面の上部に配置することはできますか? もちろん、テキストを画像として書き、回転させて使用することもできますが、今はテキストに興味があります。ありがとう。

4

10 に答える 10

42

通常どおりにテキストビューを設定できます

例えば:

 <TextView android:id="@+id/txtview"
    android:layout_height="fill_parent"
    android:layout_width="wrap_content" />

アクティビティに関数を記述して、

  • テキストの文字を逆にする
  • \nすべての文字の後に挿入

次に、テキストを TextView に設定します。

を挿入したくない場合は、同じ行に2文字が収まらず、切り捨てられないように、フォントサイズ\nを設定して再生する必要がありますandroid:layout_width

編集 私があなたを正しく理解していれば、アニメーションを使用して必要なものを得ることができます.

例えば

res/anim/myanim.xml

<rotate  xmlns:android="http://schemas.android.com/apk/res/android"
           android:fromDegrees="0" 
           android:toDegrees="-90"
           android:pivotX="50%"
           android:duration="0" />

テキストビューを配置する場所を定義するには、このファイルをいじる必要があります。

あなたの活動で:

  TextView t = (TextView)findViewById(R.id.txtview);
  String txt = "Stackoverflow";         
  t.setText(txt);

  RotateAnimation ranim = (RotateAnimation)AnimationUtils.loadAnimation(this, R.anim.myanim);
  ranim.setFillAfter(true); //For the textview to remain at the same place after the rotation
  t.setAnimation(ranim);
于 2010-05-22T16:44:42.030 に答える
25

私のために働いた:

public class VerticalTextView extends TextView {

    private int _width, _height;
    private final Rect _bounds = new Rect();

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

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

    public VerticalTextView(Context context) {
        super(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // vise versa
        _height = getMeasuredWidth();
        _width = getMeasuredHeight();
        setMeasuredDimension(_width, _height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.save();

        canvas.translate(_width, _height);
        canvas.rotate(-90);

        TextPaint paint = getPaint();
        paint.setColor(getTextColors().getDefaultColor());

        String text = text();

        paint.getTextBounds(text, 0, text.length(), _bounds);
        canvas.drawText(text, getCompoundPaddingLeft(), (_bounds.height() - _width) / 2, paint);

        canvas.restore();
    }

    private String text() {
        return super.getText().toString();
    }
}

xml:

<VerticalTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            android:background="@color/feedback_background"
            android:padding="4dip"
            android:text="@string/feedback"
            android:textColor="@color/feedback_text_color"
            android:textSize="@dimen/text_xlarge" />
于 2013-06-08T17:22:36.117 に答える
8

これを試して。それは私にとってはうまくいきます。1行のテキストを垂直に表示できますが、1行だけです。色、サイズ、パディング、余白、背景はすべて正常に機能します。

public class VerticalTextView extends TextView {

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

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

    public VerticalTextView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        final ColorStateList csl = getTextColors();
        final int color = csl.getDefaultColor();
        final int paddingBottom = getPaddingBottom();
        final int paddingTop = getPaddingTop();
        final int viewWidth = getWidth();
        final int viewHeight = getHeight();
        final TextPaint paint = getPaint();
        paint.setColor(color);
        final float bottom = viewWidth * 9.0f / 11.0f;
        Path p = new Path();
        p.moveTo(bottom, viewHeight - paddingBottom - paddingTop);
        p.lineTo(bottom, paddingTop);
        canvas.drawTextOnPath(getText().toString(), p, 0, 0, paint);
    }
}
于 2012-09-15T00:10:52.337 に答える
6

If you are using API 11 or later, you may try:

TextView t = (TextView) findViewById(R.id.txtview);
String txt = "Stackoverflow";         
t.setText(txt);
t.setRotation(90); // 90 degree rotation
于 2013-01-10T18:42:13.627 に答える
0

「Stackoverflow」を垂直に書くという質問に対する最も簡単な答えは、通常のTextViewを使用することだと思います。テキストは狭くなると次の行に折り返されるため、TextViewの幅をいじって、それぞれに1つの文字があるようにします行と、バッファとして端にさらにスペースが必要な場合は、TextView の「パディング」および/または「マージン」を増やします。

于 2019-10-25T14:19:53.293 に答える