如何解决如何将一个/两个 TextViews 设置为其侧面另一个 TextView 的高度?
嗨,我有一个包含三个 TextView 的布局。其中一个在左侧 (numPreguntaTv),另外两个在右侧(enunciadoPregTv 和 tipoPregItemTv),一个在另一个之上。
我希望 numPreguntaTv 有两行的高度,所以当 enunciadoPregTv 有两行时,它必须与左边的 numPreguntaTv 高度相同。此外,如果 enunciadoPregTv 只有一行,则tipoPregItemTv 必须适合第二行。
我想让它们完美(或几乎)对齐。
现在他们是这样的:
如果 enunciadoPregTv 有两行或更多行,那很好,它似乎与 numPreguntaTv 具有相同的高度。但是,如果 enunciadoPregTv 只有一行,则tipoPregItemTv 不能正确地适合第二行。有点下面。
我怎样才能实现我的想法?
这是我的布局:
<?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="wrap_content">
<TextView
android:id="@+id/numPreguntaTv"
android:layout_width="52dp"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-smallcaps"
android:text="10"
android:textAlignment="textEnd"
android:textColor="@color/azul"
android:textSize="48sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/enunciadoPregTv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:justificationMode="inter_word"
android:text="Lorem ipsum dolor sit amet,consectetur adipiscing elit. Pellentesque in finibus purus. Curabitur ex nisi,aliquam nec turpis quis,mollis consequat diam. In felis nibh,fringilla nec mi eu,tempus iaculis nulla."
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/numPreguntaTv"
app:layout_constraintTop_toTopOf="@+id/numPreguntaTv" />
<TextView
android:id="@+id/tipoPregItemTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="56dp"
android:text="Verdadero o falso"
android:textStyle="italic"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/enunciadoPregTv" />
<com.google.android.material.button.MaterialButton
android:id="@+id/borrarPregButton"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center_horizontal"
android:layout_marginEnd="8dp"
app:backgroundTint="@color/rojo"
app:icon="@drawable/ic_baseline_delete_24"
app:iconGravity="textStart"
app:iconPadding="0dp"
app:layout_constraintEnd_toStartOf="@+id/editarPregButton"
app:layout_constraintTop_toBottomOf="@+id/tipoPregItemTv" />
<Button
android:id="@+id/editarPregButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/editar_pregunta_button"
app:icon="@drawable/ic_baseline_edit_24"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tipoPregItemTv" />
</androidx.constraintlayout.widget.ConstraintLayout>
谢谢!!
解决方法
我认为由于您的设置,您的布局似乎(大部分)适合您。当我在 Android Studio 中加载您的布局时,这就是我得到的:
如您所见,事情看起来有些不同。
您可以将一个 TextView 基线与另一个 TextView 基线对齐,以使用 app:layout_constraintBaseline_toBaselineOf="@id/otherView"
实现您想要的效果。有两个问题:
-
多行 TextView 报告其第一行的基线,但您需要第二行的基线。
-
如果右上角的 TextView 有一行,您需要将左上角视图的基线与底部 TextView 的基线联系起来em>。这将意味着对约束的更改。
问题 #1 可以用 this 之类的东西来处理。对于问题 #2,您需要检测右上角 TextView 何时只有一行,并将左上视图的约束更改为底部视图的基线。您可以使用 ViewTreeObserver.OnGlobalLayoutListener 执行此操作。您可以更改约束,例如 this。
当然,一旦涉及到布局侦听器,就可以使用布局侦听器在左上角 TextView 的顶部添加边距,使其与第二行的基线对齐右上角的视图。这将是针对问题 #1 的,可能是我处理它的方式。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。