Android:相对侧的两个视图没有重叠,占用的空间与其内容一样小

问题描述 投票:0回答:1

我有一个非常简单但令人沮丧的问题:

我在布局中有两个视图。布局可以是任何布局,但我一直在尝试使用 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 个需求

android xml android-constraintlayout
1个回答
0
投票

我在我的一个项目中完成了这种类型设计,请检查下面的代码。

<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>
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.