约束布局:TextView文本重叠按钮

5
我第一次遇到这个问题,我的文本重叠在按钮上面。我尝试使用barrier和guidelines,但是文本显示在按钮的中心,并始终位于父级的开始位置。我希望TextView的文本从父级开头开始直到按钮之前停止,而按钮始终停留在父级的结尾,就像下面的图片一样。
enter image description here 我的布局:
 <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <TextView
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:id="@+id/titleTextView"
            android:text="title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    <TextView
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/titleTextView"
            android:id="@+id/descriptionTextView"
            android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    <ImageView
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:id="@+id/copyImageView"
            android:tint="?android:textColorSecondary"
            android:src="@drawable/ic_content_copy_black_24dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
</androidx.constraintlayout.widget.ConstraintLayout>
4个回答

13

您需要将宽度设置为0dp,这意味着match_constraint,同时将其约束到ImageView的起点。

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <TextView
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:id="@+id/titleTextView"
            android:text="title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    <TextView
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/titleTextView"
            android:id="@+id/descriptionTextView"
            android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
            app:layout_constraintEnd_toStartOf="@id/copyImageView"
            android:layout_width="0dp"
            android:layout_height="0dp"/>
    <ImageView
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:id="@+id/copyImageView"
            android:tint="?android:textColorSecondary"
            android:src="@drawable/ic_content_copy_black_24dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
</androidx.constraintlayout.widget.ConstraintLayout>

完美的答案。 - Hussnain Haidar

8

你所需要的只是一点点的间距和正确的约束

在你的descriptionTextView中,首先需要设置app:layout_constrainedWidth="true",这样它不会超出布局,并且从右侧设置一些边距,例如android:layout_marginEnd="8dp"

现在你需要设置与ImageView的约束关系,像这样:

app:layout_constraintEnd_toStartOf="@+id/copyImageView"

上面这行代码将始终保持文本视图不重叠。

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/titleTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="title"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/descriptionTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="Lorem ipsum dolor sit amet, consectetur erererereradipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
        app:layout_constrainedWidth="true"
        app:layout_constraintEnd_toStartOf="@+id/copyImageView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/titleTextView" />

    <ImageView
        android:id="@+id/copyImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_content_copy_black_24dp"
        android:tint="?android:textColorSecondary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.050000012" />

</androidx.constraintlayout.widget.ConstraintLayout>

那也是一个不错的解决方案,但是svkaka先生的解决方案更简单。感谢您的回答。 - Hussnain Haidar
2
app:layout_constrainedWidth="true" 是我要查找的内容。谢谢。 - Jetwiz

1
您可以通过添加drawable right/end来将图标添加到TextView的末尾。使用以下代码将drawable添加到TextView。您还可以设置drawable padding以在drawable和文本之间留出空间。 android:drawableRight="@drawable/search_icon"

那似乎是一个不错的解决方案,但我会坚持使用约束。 - Hussnain Haidar

0

我有类似的问题,最终得到了以下代码

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <androidx.appcompat.widget.AppCompatTextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:ellipsize="end"
        android:gravity="center"
        android:maxLines="1"
        android:text="tetetetetetetetetetetetetetetetetetetete"
        app:layout_constraintEnd_toStartOf="@+id/tvWriteReview"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/tvWriteReview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:ellipsize="end"
        android:gravity="center"
        android:maxLines="1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接