【问题标题】:How to align rotated TextView to the left of RelativeLayout如何将旋转的 TextView 对齐到 RelativeLayout 的左侧
【发布时间】:2020-05-18 00:02:28
【问题描述】:

我已将 TextView 旋转了 270 度。但问题是它从 x 轴的中点开始旋转,因此左右两边都有空间。并且由于空间的原因,文本不能在相对布局的左侧对齐。

这是我的代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="match_parent"
     android:layout_height="300dp">
     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:rotation="270"
        android:text="Sept 2015 - Dec 2016"
      />
</RelativeLayout>

这是结果。

我想像这样对齐TextView

【问题讨论】:

  • 只是一个建议:尝试将宽度更改为“12dp”之类的东西
  • 它不能解决问题@KoushikShomChoudhury

标签: android textview


【解决方案1】:

View 类具有 setPivotX() 和 setPivotY() 方法,它们设置完成旋转的点。使用这些方法设置方便的旋转点。 对应的属性是 android:transformPivotX 和 android:transformPivotY

【讨论】:

  • 你能把它添加到你的答案中吗?
  • 默认情况下,视图围绕视图中心旋转。这些属性允许设置旋转中心。 android:transformPivotX="0dp" android:transformPivotY="0dp" 表示围绕视图的左上角旋转。
【解决方案2】:

正如@Rediska 建议的那样。我加了android:transformPivotX="0dp" 它旋转了文本,但文本偏离了布局。所以我必须添加一个上边距以使其可见。现在我添加了 100dp 的边距。但要使其适用于所有长度的 TextView。我应该首先计算 textView 的宽度,然后将其应用为上边距。

更新后的代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/timeline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:rotation="270"
        android:text="Sept 2015 - Dec 2016"
        android:textSize="10dp"
        android:transformPivotX="0dp"
        android:textStyle="bold"
        android:layout_marginTop="100dp"
        />
</RelativeLayout>

结果

【讨论】:

    【解决方案3】:

    我遇到了类似的问题,但在 ConstraintLayout 中。这是我的解决方案,当然是在 RelativeLayout 中。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/leftSideTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="Left side"
            android:textSize="24sp" />
    
    
        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/rightSideTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="Right side"
            android:textSize="24sp" />
    
    </RelativeLayout>
    

    和代码:

    private fun leftSide() {
        val leftSideText = findViewById<TextView>(R.id.leftSideTextView)
        leftSideText.doOnLayout { left ->
            left.rotation = -90f
            val textSize = (left as TextView).textSize
            left.translationX = -left.width / 2.toFloat() + textSize / 2
        }
    }
    
    private fun rightSide() {
        val rightSideText = findViewById<TextView>(R.id.rightSideTextView)
        rightSideText.doOnLayout { right ->
            right.rotation = 90f
            val textSize = (right as TextView).textSize
            right.translationX = right.width / 2.toFloat() - textSize / 2
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-31
      • 1970-01-01
      • 2013-07-04
      • 1970-01-01
      • 2012-06-19
      • 2018-12-30
      • 1970-01-01
      • 2017-11-01
      相关资源
      最近更新 更多