【问题标题】:How to align view to the end of constraint layout along with a start constraint如何将视图与约束布局的末尾以及开始约束对齐
【发布时间】:2020-09-17 10:32:05
【问题描述】:

我有一个约束布局,其中一个文本视图位于左侧,另一个位于右侧。我希望正确的文本视图与末尾对齐。右侧文本视图也应该能够占用左侧的可用空间。如果空间不够,它应该在最后变成椭圆形。

我已经尝试过使用以下 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" 添加到正确的文本视图中,但这会导致两个问题:

  • 如果文本很小,则右文本视图中的文本在可用空间中居中,但我希望它右对齐。
  • 如果文本过大,则无法正确椭圆化

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

【问题讨论】:

    标签: android textview android-xml android-constraintlayout


    【解决方案1】:

    第一次使 subtitle textview width = 0dp 并将它的开始约束设置为 title textview 和 android:textAlignment="textEnd"

    第二个 title textview 结束约束到 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>
    

    【讨论】:

    • 使宽度为 0dp 是我错过的。谢谢
    【解决方案2】:

    这是你要找的吗?

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

    <?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>
    

    告诉我进展如何

    【讨论】:

      猜你喜欢
      • 2021-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      • 2018-08-30
      • 1970-01-01
      相关资源
      最近更新 更多