如何旋转TextView而不仅仅是其文本。

3
我需要旋转我的文本视图,所以我只是使用了这段代码:
<TextView
                        android:rotation="90"
                        android:layout_gravity="center"
                        android:id="@+id/price"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/stock_price"
                        android:textAppearance="@style/font"
                        android:textColor="@color/gray" />


但问题只是我的文本会旋转而不是文本视图。如下图所示:

enter image description here

如何旋转整个文本视图而不仅仅是文本?(如你在图片中看到的,“my rotated text”文本已经旋转了90度,但是textView本身仍然保持水平方向。)
我也尝试了使用动画来解决这个问题。所以我尝试了下面的代码,但是没有任何改变:
 binding.price.animate().rotation(90F).start()

非常感谢您的帮助。


你的意思是什么? - Mohamed AbdelraZek
1
我在一个水平方向的线性布局中有一个ImageView和一个TextView并排放置,我希望ImageView恰好位于TextView的末尾,但是当我旋转它时,它们之间会出现空白间隔。@MohamedAbdelraZek - mohammad
你在旋转它们两个吗? - Mohamed AbdelraZek
“both”,你是指ImageView和TextView吗?不,只是TextView。想象一下,如果有一张图片,左侧有一个竖直书写的文本。@MohamedAbdelraZek - mohammad
你好,你有这个问题的答案吗? - Anton Shkurenko
1个回答

1
你可以将它们都放在 ConstraintLayout 中并旋转整个 ViewGroup,就像这样:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:rotation="90"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/tex"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textColor="#FFFFFF"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_marginTop="8dp"
            android:src="@mipmap/ic_launcher"
            app:layout_constraintEnd_toEndOf="@+id/tex"
            app:layout_constraintStart_toStartOf="@+id/tex"
            app:layout_constraintTop_toBottomOf="@+id/tex" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

改变方向会给你这个:

enter image description here

enter image description here

更新

如果你只想旋转TextView,你需要两步操作:

1->将整个布局(包括两者)旋转"90"度。

2->通过将其旋转"-90"度来将ImageView返回到原有位置。


1
这将旋转图像视图和文本视图。我只想旋转文本视图。 - mohammad
在旋转整个布局后,@mohammad仍然是同一个人,但图像单独旋转了-90度。 - Mohamed AbdelraZek
1
不行,这样做不起作用。因为约束将设置在文本视图上,这样你只是旋转了文本视图的文本,约束仍然与之前相同。 - mohammad

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