You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中如何让垂直TextView匹配父布局高度?

问题:垂直TextView填满父布局高度的实现

我现在有如下TextView布局:

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/main_gray_text"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="#b0b4bc"
    app:layout_constraintBottom_toBottomOf="@id/plot_container"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@id/vertical_layout"
    app:layout_constraintTop_toTopOf="@id/plot_container">

    <com.google.android.material.textview.MaterialTextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:includeFontPadding="false"
        android:lineSpacingExtra="-5dp"
        android:rotation="270"
        android:text="@{map.get(12695)}"
        android:textColor="@color/white"
        android:textSize="10sp"
        android:background="@color/red_color"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="@id/gray_text_guideline"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@id/gray_text_guideline" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/gray_text_guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.5" />

</androidx.constraintlayout.widget.ConstraintLayout>

当前UI效果:

垂直TextView(红色背景)未填满灰色父布局的高度,仅占据中间部分空间

我需要满足以下要求:

  • 文本保持单行显示
  • 垂直TextView的宽度(旋转后的高度方向)填满父布局的高度

尝试过用LinearLayout解决,但没有效果。


解决方案

问题核心是旋转操作会互换View的宽高映射关系,直接设置常规布局属性无法适配旋转后的尺寸需求,以下两种方案可解决:

方案一:调整约束与旋转逻辑

移除无用的Guideline,直接将TextView约束到父布局上下边缘,同时调整宽高属性适配旋转后的尺寸:

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/main_gray_text"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="#b0b4bc"
    app:layout_constraintBottom_toBottomOf="@id/plot_container"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@id/vertical_layout"
    app:layout_constraintTop_toTopOf="@id/plot_container">

    <com.google.android.material.textview.MaterialTextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:includeFontPadding="false"
        android:rotation="270"
        android:text="@{map.get(12695)}"
        android:textColor="@color/white"
        android:textSize="10sp"
        android:background="@color/red_color"
        android:textStyle="bold"
        android:singleLine="true"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
  • 设置android:singleLine="true"强制单行
  • 让TextView未旋转时的高度填满父布局,旋转后就对应垂直方向的宽度撑满父布局高度

方案二:自定义VerticalTextView(更稳定)

重写View的测量方法,直接处理旋转后的宽高逻辑:

class VerticalTextView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : MaterialTextView(context, attrs, defStyleAttr) {

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        // 互换宽高测量逻辑,适配旋转后的显示
        super.onMeasure(heightMeasureSpec, widthMeasureSpec)
        setMeasuredDimension(measuredHeight, measuredWidth)
    }

    override fun onDraw(canvas: Canvas) {
        // 旋转画布并偏移,实现垂直文本
        canvas.rotate(-90f)
        canvas.translate(-height.toFloat(), 0f)
        super.onDraw(canvas)
    }
}

布局中使用自定义View:

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/main_gray_text"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="#b0b4bc"
    app:layout_constraintBottom_toBottomOf="@id/plot_container"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@id/vertical_layout"
    app:layout_constraintTop_toTopOf="@id/plot_container">

    <your.package.name.VerticalTextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:includeFontPadding="false"
        android:text="@{map.get(12695)}"
        android:textColor="@color/white"
        android:textSize="10sp"
        android:background="@color/red_color"
        android:textStyle="bold"
        android:singleLine="true"
        android:gravity="center" />

</androidx.constraintlayout.widget.ConstraintLayout>

内容的提问来源于stack exchange,提问作者Andrew

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 08:30:45