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

Hus*_*dar 3 android android-layout android-constraintlayout

我第一次遇到这个问题,我的文本与按钮重叠,我尝试使用屏障,指南但在按钮中心获取文本并启动父级。我正在实现 textViews 文本将从父级开始并在按钮之前结束,按钮始终保持在父级的末尾,如下图所示。
在此处输入图片说明

我的布局。

 <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>
Run Code Online (Sandbox Code Playgroud)

svk*_*aka 10

您必须将宽度设置为 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>
Run Code Online (Sandbox Code Playgroud)


Ash*_*let 6

您只需要一点边距和正确的约束

现在在你的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>
Run Code Online (Sandbox Code Playgroud)

  • app:layout_constrainedWidth="true" 是我搜索的内容。谢谢 (2认同)