在API Level >= 8中如何旋转Android TextView

11

我有一个简单的TextView

<TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:rotation="45"
   android:text="Simple text" />

在Android 2.2.2上,文字不会被旋转45度。

我看到了不同的线程,但每个人都在做动画。我不想进行动画,我只想旋转textview。


3
子类化TextView并重写onDraw方法。这非常简单。 - Blackbelt
除了“黑带”响应外,您还可以在此处检查示例https://dev59.com/EGDVa4cB1Zd3GeqPc2AQ - Yahya Arshad
请按照此教程操作:http://eleanordarephdjournal.blogspot.in/2011/03/another-good-android-example-with.html - anon
2个回答

18

在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);
    textview.startAnimation(animation);
} else {

    textview.setRotation(progress);
}

setRotation() 在 Java 中等同于 XML 中的 rotation,它们都可以旋转整个 TextView,但不会旋转 TextView 中的文本。 - twlkyao

3
我发现在SDK 21中,“android:rotation”不起作用,换句话说是在Android 5.0版本。但是我只是在布局预览中发现了这个问题,还没有在真实设备上测试。

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