在带有边距的 RelativeLayout 中旋转 TextView - TextView 仍然表现得像没有旋转一样
Rotated TextView in RelativeLayout with Margin - TextView still behaviour like if it's not rotated
我正在尝试做类似下图的事情,其中我有一个旋转 (90°) TextView
,marginLeft
与 ImageView
相关。问题是,当我增加 TextView
值时,比如说 2 到 2000000,它向右移动,表现得像一个非旋转的 TextView
(见它周围的蓝色方块)。我该如何解决?
谢谢!
代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_note"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/iv_flange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="10dp"
android:layout_marginTop="40dp"
android:adjustViewBounds="true"
android:contentDescription="@string/title"
android:scaleType="matrix"
android:src="@drawable/drawing_flange" >
</ImageView>
<TextView
android:id="@+id/tv_dimC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/iv_flange"
android:layout_alignTop="@+id/iv_flange"
android:layout_marginLeft="90dp"
android:layout_marginTop="210dp"
android:gravity="left"
android:rotation="270"
android:text="Ø1200"
android:textColor="#000"
android:textSize="14sp" />
<TextView
android:id="@+id/tv_dimd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/iv_flange"
android:layout_alignTop="@+id/iv_flange"
android:layout_marginLeft="-25dp"
android:layout_marginTop="215dp"
android:gravity="center"
android:textAlignment="center"
android:rotation="270"
android:text="Ø200000000000"
android:textColor="#000"
android:textSize="14sp" />
<TextView
android:id="@+id/tv_dime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/iv_flange"
android:layout_alignTop="@+id/iv_flange"
android:layout_marginLeft="40dp"
android:layout_marginTop="145dp"
android:gravity="center"
android:rotation="270"
android:text="300"
android:textColor="#000"
android:textSize="14sp" />
<TextView
android:id="@+id/tv_dimf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/iv_flange"
android:layout_alignTop="@+id/iv_flange"
android:layout_marginLeft="140dp"
android:layout_marginTop="-15dp"
android:gravity="center"
android:rotation="0"
android:text="75"
android:textColor="#000"
android:textSize="14sp" />
<TextView
android:id="@+id/tv_dimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/iv_flange"
android:layout_alignTop="@+id/iv_flange"
android:layout_marginLeft="190dp"
android:layout_marginTop="-15dp"
android:gravity="center"
android:rotation="0"
android:text="130"
android:textColor="#000"
android:textSize="14sp" />
</RelativeLayout>
即使您使用 xml 中的 android:rotaion
或 Java 代码中的 myTextView.setRotation()
旋转 TextView,文本视图的边界也不会更新为角度您将文本视图旋转到。为了克服这个问题,您可以编写自定义 TextView
以您需要的正确角度绘制自身。请检查 this 答案作为起点。
我正在尝试做类似下图的事情,其中我有一个旋转 (90°) TextView
,marginLeft
与 ImageView
相关。问题是,当我增加 TextView
值时,比如说 2 到 2000000,它向右移动,表现得像一个非旋转的 TextView
(见它周围的蓝色方块)。我该如何解决?
谢谢!
代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_note"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/iv_flange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="10dp"
android:layout_marginTop="40dp"
android:adjustViewBounds="true"
android:contentDescription="@string/title"
android:scaleType="matrix"
android:src="@drawable/drawing_flange" >
</ImageView>
<TextView
android:id="@+id/tv_dimC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/iv_flange"
android:layout_alignTop="@+id/iv_flange"
android:layout_marginLeft="90dp"
android:layout_marginTop="210dp"
android:gravity="left"
android:rotation="270"
android:text="Ø1200"
android:textColor="#000"
android:textSize="14sp" />
<TextView
android:id="@+id/tv_dimd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/iv_flange"
android:layout_alignTop="@+id/iv_flange"
android:layout_marginLeft="-25dp"
android:layout_marginTop="215dp"
android:gravity="center"
android:textAlignment="center"
android:rotation="270"
android:text="Ø200000000000"
android:textColor="#000"
android:textSize="14sp" />
<TextView
android:id="@+id/tv_dime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/iv_flange"
android:layout_alignTop="@+id/iv_flange"
android:layout_marginLeft="40dp"
android:layout_marginTop="145dp"
android:gravity="center"
android:rotation="270"
android:text="300"
android:textColor="#000"
android:textSize="14sp" />
<TextView
android:id="@+id/tv_dimf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/iv_flange"
android:layout_alignTop="@+id/iv_flange"
android:layout_marginLeft="140dp"
android:layout_marginTop="-15dp"
android:gravity="center"
android:rotation="0"
android:text="75"
android:textColor="#000"
android:textSize="14sp" />
<TextView
android:id="@+id/tv_dimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/iv_flange"
android:layout_alignTop="@+id/iv_flange"
android:layout_marginLeft="190dp"
android:layout_marginTop="-15dp"
android:gravity="center"
android:rotation="0"
android:text="130"
android:textColor="#000"
android:textSize="14sp" />
</RelativeLayout>
即使您使用 xml 中的 android:rotaion
或 Java 代码中的 myTextView.setRotation()
旋转 TextView,文本视图的边界也不会更新为角度您将文本视图旋转到。为了克服这个问题,您可以编写自定义 TextView
以您需要的正确角度绘制自身。请检查 this 答案作为起点。