我正在尝试创建一个两行ConstraintLayout
,每行有两个 View 。我现在遇到的问题是左 View 与右 View 重叠,正如您在屏幕截图中看到的:
这是我正在使用的代码:
<?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/