如何旋转TextView 90度并显示

sak*_*shi 32 android

我在Graph页面中有一个情况,LinearLayout应该显示TextView旋转90度.

vij*_*jay 55

试试这个::

<TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:rotation="-95"
                android:text="2" 
                />
Run Code Online (Sandbox Code Playgroud)

  • 这种方法的问题是宽度大小不会改变.知道怎么克服这个吗? (10认同)
  • 供参考。需要Build.VERSION.SDK_INT &gt;11 (2认同)

Rot*_*miz 38

最快最方便的方法是Rotate通过Animation

像往常一样在常规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" />
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)


Sim*_*ges 13

在android中任何新视图都有一个名为setRotation(float)的方法可以使用它

textview.setRotation(float);
Run Code Online (Sandbox Code Playgroud)

但请注意,此方法已在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);
}
Run Code Online (Sandbox Code Playgroud)


小智 10

 <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" />
Run Code Online (Sandbox Code Playgroud)


use*_*815 6

[解决]经过一年的努力,我终于在论坛上对这个问题进行了分类!

这里的技巧是硬编码TextView比文本更大的layout_width和layout_height- 例如100dp x 100dp。

然后将TextView放到中FrameLayout,关闭的剪裁,FrameLayout android:clipChildren="false"并剪裁到TextView的填充android:clipToPadding="false"

TextView 具有硬编码的高度和宽度

现在TextView将漂浮在中FrameLayout。使用TextView重力设置在文本的边界内移动文本。

然后使用layout_gravity设置在其中父对象FrameLayout内移动边界:

这是左右对齐文本旋转-90度的示例:

解决方案示例

[更新]包含旋转文本的框架视图的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>
Run Code Online (Sandbox Code Playgroud)