android - 两行约束布局

标签 android android-constraintlayout

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

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>

我希望左侧 View (标题、日期)停止在下载图像和持续时间开始的位置。我没有太多使用 ConstraintLayouts,所以这可能很简单。

最佳答案

有很多不同的方法可以实现此目的,一种方法是将文本限制在图像中并赋予它们相同的高度。

如果您执行以下操作,它们将不会相互重叠:

在您的示例中,您在文本上使用了 android:layout_width="match_parent",因此它被展开,抛出所有行并重叠您的其他 View 。
您应该使用 android:layout_width="0dp"(它是match_constraint),这样您的 View 就不会相互重叠。

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

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


    <TextView
        android:id="@+id/textView4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="loot at this text that wont everlap the image"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/imageView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintBottom_toBottomOf="@+id/textView4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/textView4"
        tools:src="@tools:sample/avatars[1]" />
</androidx.constraintlayout.widget.ConstraintLayout>

关于android - 两行约束布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55317794/

相关文章:

java - 安卓工作室 : Specify location of gradle or android eclipse project

java - Android 将一个 fragment 导航到另一个 fragment 可以正常工作,但是看不到其他 fragment 的背景

android - 如何更改 NavigationView 的项目文本大小?

android - 如果 ConstraintLayout 中的文本太长,TextView 会超出 Constraint

android - 如何在约束布局中链接组?

android - 我在约束布局上遇到膨胀异常,我不知道为什么

android - 将属性从约束布局中的一个 View 隐藏到另一个 View

具有包装内容行为的 androidx Recycler View 匹配约束(0dp)

android - 在 fragment 中显示graphview仅在应用程序重启后显示数据

android - java.lang.IllegalStateException : Unable to create layer for X -- when using hardware layers