android - 约束布局 - 当 Textview 变大时会与其他 View 重叠

标签 android android-constraintlayout

This is how i want layout to look like.

上图显示了我希望我的 View 看起来像什么。 问题是当上重力文本的大小增加时,它会将图像推出 View 。我尝试使用障碍,但无法使其发挥作用。 下图显示,我已经实现了多远。但现在的问题是图像总是卡在最后。但我希望它紧挨着引力文本,当该文本增加时,它应该卡在最后并且引力文本的高度应该增加。

Current working code image

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="@dimen/dp_4">

            <TextView
                android:id="@+id/last_msg_tv"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                android:textAlignment="viewStart"
                android:textColor="@color/color_grey_3"
                android:textSize="@dimen/sp_12"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="@+id/barrier9"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="@+id/title_tv"
                app:layout_constraintTop_toBottomOf="@+id/title_tv"
                tools:text="In the future, Earth is slowly becoming uninhabitable. Ex-NASA pilot Cooper, along with a team of researchers, is sent on a planet exploration mission to report which planet can sustain life." />

            <TextView
                android:id="@+id/title_tv"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:textColor="@color/color_grey_2"
                android:textSize="@dimen/sp_16"
                android:textStyle="bold"
                app:layout_constraintEnd_toStartOf="@+id/imageView4"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:text="Alpha CapriCod A" />

            <ImageView
                android:id="@+id/imageView4"
                android:layout_width="@dimen/dp_24"
                android:layout_height="@dimen/dp_24"
                android:layout_marginEnd="8dp"
                android:tint="@color/color_grey_5"
                app:layout_constraintEnd_toStartOf="@+id/user_count_tv"
                app:layout_constraintTop_toTopOf="@+id/title_tv"
                app:srcCompat="@drawable/mutiple_user_img" />

            <TextView
                android:id="@+id/user_count_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                app:layout_constraintBottom_toBottomOf="@+id/imageView4"
                app:layout_constraintEnd_toEndOf="@+id/barrier9"
                app:layout_constraintTop_toTopOf="@+id/imageView4"
                tools:text="1000" />

            <android.support.constraint.Barrier
                android:id="@+id/barrier9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:barrierDirection="left"
                tools:layout_editor_absoluteX="387dp"
                tools:layout_editor_absoluteY="8dp" />

        </android.support.constraint.ConstraintLayout>

    </android.support.v7.widget.CardView>

</android.support.constraint.ConstraintLayout>

最佳答案

你可以做的是将 title_tv(宽度为 wrap_content)、imageView4user_count_tv 水平放置使用 packed 样式和 0 偏置链接,使它们向左对齐。当 title_tv 扩展时,您需要使用 app:layout_constrainedWidth="true" 来防止它把其他 View 推到边界之外。约束应该是这样的:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="4dp">

            <TextView
                android:id="@+id/last_msg_tv"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:textAlignment="viewStart"
                android:textSize="12sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="@id/title_tv"
                app:layout_constraintTop_toBottomOf="@id/title_tv"
                tools:text="In the future, Earth is slowly becoming uninhabitable. Ex-NASA pilot Cooper, along with a team of researchers, is sent on a planet exploration mission to report which planet can sustain life." />

            <TextView
                android:id="@+id/title_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:textSize="16sp"
                android:textStyle="bold"
                app:layout_constraintHorizontal_bias="0"
                app:layout_constraintHorizontal_chainStyle="packed"
                app:layout_constrainedWidth="true"
                app:layout_constraintEnd_toStartOf="@id/imageView4"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:text="Alpha CapriA" />

            <ImageView
                android:id="@+id/imageView4"
                android:layout_width="24dp"
                android:layout_height="24dp"
                android:layout_marginEnd="8dp"
                app:layout_constraintEnd_toStartOf="@id/user_count_tv"
                app:layout_constraintStart_toEndOf="@id/title_tv"
                app:layout_constraintTop_toTopOf="@id/title_tv"
                app:srcCompat="@android:drawable/btn_star" />

            <TextView
                android:id="@+id/user_count_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                app:layout_constraintBottom_toBottomOf="@id/imageView4"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@id/imageView4"
                app:layout_constraintTop_toTopOf="@+id/imageView4"
                tools:text="1000" />

        </android.support.constraint.ConstraintLayout>

    </android.support.v7.widget.CardView>

</android.support.constraint.ConstraintLayout>

结果:

enter image description here

还有更长的标题:

enter image description here

关于android - 约束布局 - 当 Textview 变大时会与其他 View 重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54386844/

相关文章:

Android 开放附件协议(protocol)和 MTP

android - 如何在android中使用java更改约束布局背景图像

android - android 6 上的 ConstraintLayout 问题

android - 在 ConstraintLayout 中将动态大小的 View 约束到 View 的底部

android - LinearLayout 中的 ConstraintLayout 不起作用

安卓 onTouchListener : Switch Activity On User Touch

java - Android 从 URL 设置 ListView 图片

android - 迁移到androidx : Didn't find class "androidx.constraintlayout.ConstraintLayout" on path: DexPathList后

从底部开始的 Android gridview 堆栈

android - 快速联系人徽章无法使用 Intent i=new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI) startActivityForResult(i 0) 传递 Intent