我有一个非常简单但令人沮丧的问题:
我在布局中有两个视图。布局可以是任何布局,但我一直在尝试使用 ConstraintLayout。
我想要这两个视图在相反的两侧,如下所示:
我想要意见:
这是代码:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/view2"/>
<TextView
android:id="@+id/view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我尝试搜索 StackOverflow,但没有任何问题可以处理我列出的所有场景。请帮忙。
在我所有的不同尝试中,我都没能满足所有 4 个需求
我在我的一个项目中完成了这种类型设计,请检查下面的代码。
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Left TextView -->
<TextView
android:id="@+id/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left View"
android:maxLines="2" <!-- Ensures text wraps if needed -->
android:ellipsize="end"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/view2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:paddingStart="16dp"
android:paddingEnd="8dp" />
<!-- Right TextView -->
<TextView
android:id="@+id/view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right View"
android:maxLines="2"
android:ellipsize="end"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:paddingStart="8dp"
android:paddingEnd="16dp" />
</androidx.constraintlayout.widget.ConstraintLayout>