约束布局 - 当文本视图增大时,会与其他视图重叠

3

我想要的布局

以上图像显示了我想要的视图布局。 问题是,当上方重力文本增大时,它会将图像推出视图。我尝试使用障碍物解决,但无法实现。 下面的图像显示了我已经取得的进展。但现在的问题是图像总是固定在末尾。但我希望它紧贴着重力文本,当文本增加时,它应该固定在末尾,而重力文本应该增加高度。

当前工作代码图像

<?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>
2个回答

16

您可以将title_tv(宽度设置为wrap_content),imageView4user_count_tv放在一个水平链上,并使用packed风格和偏移量0使它们对齐到左侧。当title_tv扩展时,您需要使用app:layout_constrainedWidth="true"来防止其将其他视图推出边界。以下是约束应该看起来的样子:

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

结果:

在此输入图片描述

并且带有更长的标题:

在此输入图片描述


非常有用,谢谢。 - King Of The Jungle
完美运作 - Beto

0

可以尝试以下方法:
1. 在您的卡片视图的约束布局中,添加一个方向为水平的LinearLayout。
2. 在该水平视图中,添加您的标题,并将其宽度和高度设置为wrap context。然后添加ImageView和Number TextView。
3. 将详细信息TextView设置在该线性布局下方。
完成。


我必须严格使用 ConstraintLayout,不能使用 LinearLayout 或 RelativeLayout。添加这两个会破坏使用 Constraint 的目的。 - kartic chaudhary

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