两行ConstraintLayout

时间:2019-03-23 19:57:02

标签: android android-constraintlayout

我正在尝试创建两行ConstraintLayout,每行有两个视图。我现在遇到的问题是左视图与右视图重叠,如屏幕截图所示:

enter image description here

这是我正在使用的代码:

<?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="wrap_content"
    android:layout_gravity="center"
    android:padding="5dp"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <ImageView
        android:id="@+id/download"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:paddingStart="5dp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        />

    <TextView
        android:id="@+id/date"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="13sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/title"
        android:layout_marginTop="4dp"
        />

    <TextView
        android:id="@+id/duration"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="13sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/title"
        android:layout_marginTop="4dp"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

我希望左侧视图(标题,日期)停止在下载图像和持续时间的开始位置。我与ConstraintLayouts的合作不多,所以可能很容易。

1 个答案:

答案 0 :(得分:1)

有很多不同的方法可以实现这一目标,一种方法是将文本约束到图像中并赋予它们相同的高度。

如果您要执行以下操作,它们将不会彼此重叠:

在您的示例中,您在文本上使用了android:layout_width="match_parent",因此将其展开并抛出了所有行并覆盖了其他视图。
您应该使用android:layout_width="0dp"(它是{{1 }}),这样您的视图就不会重叠。

这是一个布局示例,看起来像您要实现的行:

match_constraint