【问题标题】:Stop views from overlapping in ConstraintLayout阻止视图在 ConstraintLayout 中重叠
【发布时间】:2019-07-03 16:17:17
【问题描述】:

我正在尝试使用 ConstrainLayout 构建一个包含三个视图的简单布局:

当左视图中的文本很长时,我想看到这个:

但我得到的是这个 - 左视图向右增长太多并且隐藏了中间视图。

这是我的代码:

<android.support.constraint.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="match_parent">

    <TextView
            android:id="@+id/left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    
    <TextView
            android:id="@+id/middle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintLeft_toRightOf="@id/left"
            app:layout_constraintTop_toTopOf="parent"/>
    
    <TextView
            android:id="@+id/right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>

</android.support.constraint.ConstraintLayout>

到目前为止我尝试过的一些事情:

  • 制作链条并尝试不同的链条样式
  • 将 android:minWidth 设置为中间视图以防止它被左视图压扁
  • 使用 Guidline 防止左侧和/或中间视图过度向右扩展

我花了大约 4 个小时试图让事情顺利进行,但到目前为止还没有成功。非常感谢帮助。

【问题讨论】:

    标签: android android-constraintlayout


    【解决方案1】:

    您缺少的是app:layout_constrainedWidth="true",它对Views 实施约束,宽度设置为wrap_content。我会将前两个TextView 链接到packed 样式和偏置o 0.0 以将链对齐到父级的左侧并将其约束到右侧的第三个TextView。右边的TextView 可以在右边的约束下保持自己的状态。

    例子(假设只有左边的TextView会变大):

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.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="match_parent">
    
        <TextView
                android:id="@+id/left"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="left"
                app:layout_constrainedWidth="true"
                app:layout_constraintHorizontal_chainStyle="packed"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toLeftOf="@id/middle"
                app:layout_constraintTop_toTopOf="parent"/>
    
        <TextView
                android:id="@+id/middle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="middle"
                app:layout_constraintLeft_toRightOf="@id/left"
                app:layout_constraintRight_toLeftOf="@id/right"
                app:layout_constraintTop_toTopOf="parent"/>
    
        <TextView
                android:id="@+id/right"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="right"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintRight_toRightOf="parent"/>
    
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

    • 太好了,这是唯一有效的答案。在所有其他答案中,视图不会将自己包裹在文本周围,而是会拉伸以填充父视图的整个宽度。
    • 以我需要的精度工作。解决方案。
    • 你拯救了我的一天,伙计
    【解决方案2】:

    使用链没有问题,您只需将视图宽度设置为match_constraint,这样它就不会与您的其他视图重叠:

      <TextView
        android:id="@+id/left"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="this is some text"
        app:layout_constraintEnd_toStartOf="@+id/right"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    <TextView
        android:id="@+id/middle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="this is some text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/right"
        app:layout_constraintTop_toTopOf="@+id/right" />
    
    <TextView
        android:id="@+id/right"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="this is some text but longgggggggggggggggggggggggggggggggggggggggggggggggggggggg"
        app:layout_constraintEnd_toStartOf="@+id/middle"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/left"
        app:layout_constraintTop_toTopOf="@+id/left" />
    

    看起来像这样:

    你 95% 的方法是正确的,所有这些都需要改变你的视图宽度。

    【讨论】:

      【解决方案3】:

      您可以使用权重来划分文本视图之间的水平间距,如下所示

      <android.support.constraint.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="match_parent">    
      
      <TextView
          android:text="A long text to show how weights work in constraint layouts"
          android:id="@+id/one"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:background="#caf"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintLeft_toLeftOf="parent"
          app:layout_constraintRight_toLeftOf="@+id/two"
          app:layout_constraintHorizontal_weight="1"/>
      
      <TextView
          android:text="Short text in middle"
          android:id="@+id/two"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:background="#fac"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintLeft_toRightOf="@+id/one"
          app:layout_constraintRight_toRightOf="@id/three"
          app:layout_constraintHorizontal_weight="1"/>
      
      <TextView
          android:text="Here is the end to the right"
          android:id="@+id/three"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:background="#fea"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintLeft_toRightOf="@+id/two"
          app:layout_constraintRight_toRightOf="parent"
          app:layout_constraintHorizontal_weight="1"/>
      
      </android.support.constraint.ConstraintLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-25
        • 1970-01-01
        • 1970-01-01
        • 2018-02-12
        相关资源
        最近更新 更多