如何将视图对齐到约束布局的末尾以及开始约束

How to align view to the end of constraint layout along with a start constraint

我有一个约束布局,一个文本视图在左侧,另一个在右侧。我希望正确的文本视图对齐到最后。右侧文本视图也应该能够在左侧获取可用的 space。如果 space 不够,它应该在最后变成椭圆形。

我已经尝试使用以下 xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:padding="@dimen/dimen_16dp">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:text="Title"
        android:textStyle="bold"
        android:layout_marginEnd="@dimen/dimen_24dp"/>

    <TextView
        android:id="@+id/subtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="Subtitle"/>

</androidx.constraintlayout.widget.ConstraintLayout>

这对于右侧文本视图中的小文本非常有用。但是,如果右侧文本视图 中的 文本很大,它会与左侧文本视图重叠。我尝试将 app:layout_constraintStart_toEndOf="@id/title" 添加到正确的文本视图中,但它导致了两个问题:

  • 如果文本很小,右侧文本视图中的文本会在可用的位置居中 space,但我希望它右对齐。
  • 如果文本很大,则椭圆化不正确

如何在不将父布局更改为线性布局的情况下实现

这是你要找的吗

现在左边的文字现在不重叠了。

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <TextView
        android:id="@+id/title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="24dp"
        android:text="Large text which could overlap"
        android:textSize="28sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toStartOf="@id/subtitle"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/subtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="Subtitle"
        android:textSize="28sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

告诉我进展如何

1st make subtitle textview width = 0dp and give it's start constraint to end of title textview and android:textAlignment="textEnd"

2nd title textview end constraint to start of subtitle textview

    <?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="24dp"
        android:text="Title"
        android:textStyle="bold"
        app:layout_constraintEnd_toStartOf="@+id/subtitle"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/subtitle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="5dp"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="Subtitle Subtitle Subtitle Subtitle Subtitle Subtitle Subtitle"
        android:textAlignment="textEnd"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/title"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>