我在图形页面遇到一个情况,需要LinearLayout
显示旋转了90度的TextView
。
我在图形页面遇到一个情况,需要LinearLayout
显示旋转了90度的TextView
。
试试这个:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:rotation="-95"
android:text="2"
/>
最快、最方便的方法是使用Animation
对普通的TextView进行Rotate
旋转。
可以像下面这样在常规TextView上使用旋转动画。
rotateAnimation.xml:
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="-90"
android:pivotX="50%"
android:duration="0"
android:fillAfter="true" />
Java代码:
TextView text = (TextView)findViewById(R.id.txtview);
text.setText("rotated text here");
RotateAnimation rotate= (RotateAnimation)AnimationUtils.loadAnimation(this,R.anim.rotateAnimation);
text.setAnimation(rotate);
在Android中,对于任何新视图,都有一个名为setRotation(float)的方法,您可以使用它。
textview.setRotation(float);
但请注意,此方法是在API级别11中添加的。
因此,如果您想要支持它,您可以使用此方法。
if (Build.VERSION.SDK_INT < 11) {
RotateAnimation animation = new RotateAnimation(oldAngel, newAngel);
animation.setDuration(100);
animation.setFillAfter(true);
watermarkText.startAnimation(animation);
} else {
watermarkText.setRotation(progress);
}
<TextView
android:id="@+id/rotated_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:alpha=".3"
android:background="@drawable/grey_capsule"
android:gravity="center"
android:textColor="@android:color/white"
android:textSize="14sp"
android:padding="4dp"
android:layout_marginLeft="-50dp"
android:rotation="-90"
android:text="Andrew Coder" />
TextView
的 layout_width 和 layout_height 设为比实际文本更大的固定值,例如 100dp x 100dp。TextView
放置在 FrameLayout
中,并关闭 FrameLayout
的裁剪功能 android:clipChildren="false"
,关闭 TextView 的内边距裁剪 android:clipToPadding="false"
:
TextView
with hard-coded height and width
现在 TextView
将会漂浮在 FrameLayout
中。使用 TextView
的gravity属性设置文本在其范围内的移动位置。FrameLayout
内移动范围: <FrameLayout
android:layout_width="@dimen/item_col_width_val"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:padding="@dimen/table_header_margin">
<TextView
android:layout_width="@dimen/table_title_row_height"
android:layout_height="@dimen/table_title_row_height"
android:layout_gravity="bottom|end"
android:gravity="bottom"
android:rotation="-90"
android:text="@string/asm_col_title_in_stock"
android:textColor="@color/colorGood" />
</FrameLayout>
如果您不需要动画,则可以尝试类似 问题 中提供的解决方案。文本将从底部向上编写。
它支持所有基本的 TextView 特性:
主要重点是根据文本参数覆盖 onDraw()
和 onMeasure()
方法:
@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);
}
}
@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));
}
}