是否有可能在android中的textview中垂直写入?

比方说,你有一个正常的TextView,写有“Stackoverflow”,是否可以旋转TextView -90°,让S在底部,W在屏幕顶部? 当然,我可以把我的文字写成一个图像,旋转并使用它,但我现在对文本感兴趣。 谢谢。

你可以像往常一样设置你的textview

例如:

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

并在你的活动中写一个函数

  • 扭转文字中的字符
  • 在每个字符后面插入\n

然后将文本设置为TextView。

如果你不想插入\n ,你将不得不设置android:layout_width的大小,并使用字体大小不要在同一行上放置2个字符,并且不能截断

编辑如果我已经正确理解了你,你可以通过使用animation来得到你想要的。

例如

res/animation/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); 

为我工作:

 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" /> 

尝试这个。 它对我来说工作正常。 它可以垂直显示一行文本,而只显示一行。 颜色,大小,填充,边距和背景都可以正常工作。

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

如果您使用API​​ 11或更高版本,则可以尝试:

 TextView t = (TextView) findViewById(R.id.txtview); String txt = "Stackoverflow"; t.setText(txt); t.setRotation(90); // 90 degree rotation 

我们可以使用XML视图设置旋转

  <TextView android:id="@+id/txtview" android:rotation="-90" android:text="123" android:layout_height="wrap_content" android:layout_width="wrap_content" /> 

我在另一个StackOverflow 问题中提供了一个解决scheme。 您可以通过从View扩展并重写其onMeasure()onDraw()方法来获得垂直TextView。 但是,它不会支持所有的TextViewfunction,而是支持所有的TextViewfunction,如填充,大小,颜色和字体。

 import android.annotation.TargetApi; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.Typeface; import android.os.Build; import android.text.TextPaint; import android.util.AttributeSet; import android.util.Log; import android.util.TypedValue; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class VerticalLabelView extends View { private final String LOG_TAG = "VerticalLabelView"; private int _ascent = 0; private int _leftPadding = 0; private int _topPadding = 0; private int _rightPadding = 0; private int _bottomPadding = 0; private int _textSize = 0; private int _measuredWidth; private int _measuredHeight; private Rect _textBounds; private TextPaint _textPaint; private String _text = ""; private TextView _tempView; private Typeface _typeface = null; public VerticalLabelView(Context context) { super(context); initLabelView(); } public VerticalLabelView(Context context, AttributeSet attrs) { super(context, attrs); initLabelView(); } public VerticalLabelView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initLabelView(); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public VerticalLabelView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); initLabelView(); } private final void initLabelView() { this._textBounds = new Rect(); this._textPaint = new TextPaint(); this._textPaint.setAntiAlias(true); this._textPaint.setTextAlign(Paint.Align.CENTER); } public void setText(String text) { this._text = text; requestLayout(); invalidate(); } public void setPadding(int padding) { setPadding(padding, padding, padding, padding); } public void setPadding(int left, int top, int right, int bottom) { this._leftPadding = left; this._topPadding = top; this._rightPadding = right; this._bottomPadding = bottom; requestLayout(); invalidate(); } public void setTextSize(int size) { this._textSize = size; this._textPaint.setTextSize(size); requestLayout(); invalidate(); } public void setTextColor(int color) { this._textPaint.setColor(color); invalidate(); } public void setTypeFace(Typeface typeface) { this._typeface = typeface; this._textPaint.setTypeface(typeface); requestLayout(); invalidate(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { try { this._textPaint.getTextBounds(this._text, 0, this._text.length(), this._textBounds); this._tempView = new TextView(getContext()); this._tempView.setPadding(this._leftPadding, this._topPadding, this._rightPadding, this._bottomPadding); this._tempView.setText(this._text); this._tempView.setTextSize(TypedValue.COMPLEX_UNIT_PX, this._textSize); this._tempView.setTypeface(this._typeface); this._tempView.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); this._measuredWidth = this._tempView.getMeasuredHeight(); this._measuredHeight = this._tempView.getMeasuredWidth(); this._ascent = this._textBounds.height() / 2 + this._measuredWidth / 2; setMeasuredDimension(this._measuredWidth, this._measuredHeight); } catch (Exception e) { setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); Log.e(LOG_TAG, Log.getStackTraceString(e)); } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (!this._text.isEmpty()) { float textHorizontallyCenteredOriginX = this._measuredHeight / 2f; float textHorizontallyCenteredOriginY = this._ascent; canvas.translate(textHorizontallyCenteredOriginY, textHorizontallyCenteredOriginX); canvas.rotate(-90); canvas.drawText(this._text, 0, 0, this._textPaint); } } }