如何将TextView旋转90度并显示

43

我在图形页面遇到一个情况,需要LinearLayout显示旋转了90度的TextView


我查看了这个并且解决了我的问题:https://dev59.com/DnM_5IYBdhLWcg3wt1rD - sakshi
从API 11(Android 3.0)开始,可以在XML中实现此操作。https://dev59.com/32865IYBdhLWcg3whe5f - chobok
6个回答

70

试试这个:

<TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:rotation="-95"
                android:text="2" 
                />

2
请注意:需要有 Build.VERSION.SDK_INT >11。 - MilapTank
26
这种方法的问题在于宽度大小不会改变。有什么办法可以克服这个问题吗? - abh22ishek
@abh22ishek 我找到了一个解决宽度/高度混淆问题的方法。基本上,我将TextView包装在RelativeLayout中,并使用负边距。等我有时间时,我会发布完整的答案。 - xjcl

42

最快、最方便的方法是使用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);

这很好,但并没有对我有太大帮助。查看这个网址https://dev59.com/DnM_5IYBdhLWcg3wt1rD,在我的计算机科学工程中给了我很多帮助。 - sakshi

17

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

1
更详细的解释会更好。 - Avi Turner
我编辑了答案以提供更多信息。 - Simon K. Gerges

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

11
[已解决] 在我的待办清单上挂了一年,而论坛上也没有合适的答案,我终于解决了这个问题!
关键在于将 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属性设置文本在其范围内的移动位置。
然后使用layout_gravity属性在父级 FrameLayout 内移动范围:
以下是一个右对齐和底部对齐的旋转-90度的文本示例: Solution example [更新] 带有旋转文本的框架视图的示例 XML:
       <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>

请分享你的 XML 代码,这样我们可以更快地理解你对答案的解释。 - ProgDevCode
@ProgDevCode 根据要求添加了一些 XML。 - user7653815

0

如果您不需要动画,则可以尝试类似 问题 中提供的解决方案。文本将从底部向上编写。

它支持所有基本的 TextView 特性:

  • 文本大小
  • 文本颜色
  • 文本字体
  • 文本填充
  • 通过 XML 使用

主要重点是根据文本参数覆盖 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));
        }
    }

请查看Android中的垂直(旋转)标签以查看完整的源代码。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接