【问题标题】:One Textview overlaps another in ConstraintLayout一个 Textview 在 ConstraintLayout 中与另一个重叠
【发布时间】:2020-02-27 17:28:17
【问题描述】:

我有 ConstraintLayout,里面有两个 TextView:

<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">
    <TextView
        android:id="@+id/first"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/second"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/first"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</androidx.constraintlayout.widget.ConstraintLayout>

我想将第一个放在父容器的左上角,并将第二个放在父容器的右上角。但是两个文本视图都可以包含任意长度的文本。当第一个文本很长时,它会与第二个文本视图重叠。我可以通过在 LinearLayout 中包装两个文本视图来解决这个问题。但这种方式在我看来并不优雅。也许还有另一种方法可以做到这一点?我的意思是 ConstraintLayout 的功能

【问题讨论】:

  • 你可以改用相对布局
  • 你需要水平链和宽度与0dp

标签: android android-constraintlayout


【解决方案1】:

首先,从宽度中删除wrap_content,因为这会使左/右约束无关紧要。在其位置使用0dp 以使其遵守约束规则。

然后在视图之间创建一个水平链并将其命名为spread,这样视图就不会在任何点重叠,并且它们也始终停留在屏幕边缘。

最后,相应地对齐文本,使左侧对齐到视图的开头,右侧对齐到视图末尾。在下面的示例中,您会看到无论文本有多长,视图都不会重叠并粘在它们的两侧。

注意: 使用开始/结束约束(如在我的代码中)而不是左/右被认为是更好的做法,以适应具有不同文本方向的设备。您还可以通过添加适当的边距来修改下面的代码,使文本远离屏幕边缘。

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/first"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textAlignment="viewStart"
        app:layout_constraintEnd_toStartOf="@+id/second"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="@tools:sample/lorem/random" />

    <TextView
        android:id="@+id/second"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textAlignment="viewEnd"
        app:layout_constrainedWidth="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintStart_toEndOf="@id/first"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="@tools:sample/lorem/random" />

</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

  • JIC 这有助于其他一些方面,但在开始/结束约束上使用左/右可能会产生不同的结果。切换到开始/结束解决了我的问题。
【解决方案2】:

使用这个

 <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">
    <TextView
        android:id="@+id/first"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="chauhan"/>
    <TextView
        android:id="@+id/second"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="mehul"/>
</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

    【解决方案3】:
    <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">
        <TextView
            android:id="@+id/first"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/second"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/first"
            app:layout_constraintRight_toRightOf="parent"
            android:gravity="right"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    这样第二个 TextView 肯定不会与第一个重叠,但可能会发生第一个太长以至于没有第二个的地方。如果是这种情况,我建议将 maxWidth 设置为第一个 TextView,以便在最坏的情况下为第二个 TextView 留出一些空间。

    我还向第二个添加了 graviti 以将文本保留在 TextView 的右侧。

    【讨论】:

      【解决方案4】:
      <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">
          <TextView
              android:id="@+id/first"
              app:layout_constraintTop_toTopOf="parent"
              app:layout_constraintLeft_toLeftOf="parent"
              android:layout_width="0dp"
              app:layout_constraintRight_toLeftOf="@+id/second"
              android:layout_height="wrap_content" />
          <TextView
              android:id="@+id/second"
              app:layout_constraintTop_toTopOf="parent"
              app:layout_constraintLeft_toRightOf="@+id/first"
              app:layout_constraintRight_toRightOf="parent"
              android:layout_width="0dp"
              android:layout_height="wrap_content" />
      </androidx.constraintlayout.widget.ConstraintLayout>
      

      【讨论】:

        【解决方案5】:
         <androidx.constraintlayout.widget.ConstraintLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">
        
                  <androidx.appcompat.widget.AppCompatTextView
                            android:id="@+id/tvFirst"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            app:layout_constraintEnd_toStartOf="@+id/tvSecond"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toTopOf="parent" />
        
                        <androidx.appcompat.widget.AppCompatTextView
                            android:id="@+id/tvSecond"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintStart_toEndOf="@+id/tvFirst"
                            app:layout_constraintTop_toTopOf="parent" />
        
                    </androidx.constraintlayout.widget.ConstraintLayout>
        

        【讨论】:

          猜你喜欢
          • 2020-12-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-17
          • 2013-06-30
          • 2020-12-06
          • 1970-01-01
          相关资源
          最近更新 更多