[英]Align View to the top of a rotated TextView
I have a TextView
containing some text that is displayed with android:rotation="270"
so it reads from the bottom to the top. 我有一个
TextView
包含一些用android:rotation="270"
显示的文本,所以它从底部到顶部读取。
I have another View in the layout (a Button
, ImageView
, or some other simple View
object). 我在布局中有另一个View(
Button
, ImageView
或其他一些简单的View
对象)。
I want to align the second view to the top of the rotated text like so: 我想将第二个视图对齐到旋转文本的顶部,如下所示:
I'm using ConstraintLayout
so I tried the obvious, assigning constraintBottom_toTopOf="@id/text"
to the view. 我正在使用
ConstraintLayout
所以我尝试了明显的,为视图分配constraintBottom_toTopOf="@id/text"
。 However, it does not align the View
to the wanted positon but to the top of the TextView
in its unrotated position. 但是,它不会将
View
与所需的位置对齐,而是将其与未旋转位置的TextView
顶部对齐。 It's probably because (as you can see on the image) the bounding box of the TextView
does not get rotated, only the displayed text does. 这可能是因为(正如您在图像上看到的那样)
TextView
的边界框不会旋转,只有显示的文本才会旋转。
Is there any way to get the View
to align to the actual top of the TextView
, on top of its last character without hardcoding Translation
values? 有没有什么办法让
View
,调整到实际顶部TextView
,其最后一个字符的顶部没有硬编码Translation
值? Or do I have to work around it from code? 或者我是否必须从代码中解决它?
Here is my simple layout to reproduce the issue: 这是我重现问题的简单布局:
<?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"
tools:context=".MainActivity">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="50sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:rotation="270"/>
<Button
android:layout_width="200dp"
android:layout_height="64dp"
android:text="Align me!"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@id/text"/>
</androidx.constraintlayout.widget.ConstraintLayout>
I found two solutions: 我找到了两个解决方案
Writing a simple custom control that inherits from TextView: 编写一个继承自TextView的简单自定义控件:
public class VerticalTextView extends TextView {
final boolean topDown;
public VerticalTextView(Context context,
AttributeSet attrs) {
super(context, attrs);
final int gravity = getGravity();
if (Gravity.isVertical(gravity) && (gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) {
setGravity( (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) | Gravity.TOP );
topDown = false;
} else {
topDown = true;
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
@Override
protected void onDraw(Canvas canvas) {
if (topDown) {
canvas.translate(getWidth(), 0);
canvas.rotate(90);
} else {
canvas.translate(0, getHeight());
canvas.rotate(-90);
}
canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());
getLayout().draw(canvas);
}
}
Usint this library . Usint这个图书馆 。 How to use?
如何使用?
Add in your dependencies in build.gradle file: 在build.gradle文件中添加依赖项:
dependencies {
compile 'rongi.rotate-layout:rotate-layout:2.0.0'
}
XML file: XML文件:
<com.github.rongi.rotate_layout.layout.RotateLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:angle="270"> <!-- Specify rotate angle here -->
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="50sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</com.github.rongi.rotate_layout.layout.RotateLayout>
<Button
android:layout_width="200dp"
android:layout_height="64dp"
android:text="Align me!"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@id/text"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.