【问题标题】:Move view away from overlapping in ConstraintLayout (minimum constraint)将视图从 ConstraintLayout 中的重叠移开(最小约束)
【发布时间】:2021-09-25 14:09:30
【问题描述】:

在某些屏幕上,文本和图像重叠。在这种情况下,我想将文本向上移动。

我使用指南将 TextView 放置在百分比位置。这是文本的首选位置。在它下面是一个带有 layout_width="match_parent" 和比例的图像。根据屏幕纵横比,图像将变得足够高以与文本重叠。在这种情况下,我希望文本向上移动。所以我想要文本和图像之间的“最小约束”为 8dp。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guidelineGoldenRatio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.381966" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Keep me from overlapping"
        app:layout_constraintTop_toTopOf="@+id/guidelineGoldenRatio"
        app:layout_constraintBottom_toTopOf="@+id/guidelineGoldenRatio"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:adjustViewBounds="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/ic_supercat_popular" />

</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

    标签: android android-constraintlayout


    【解决方案1】:

    您可以使用Barrier 小部件。将障碍物的方向设置为“顶部”,并将参考 ID 设置为您的图像和指南。 (如果您无法引用指南,请将 Space 小部件约束到指南,并将 Space 小部件的 id 设置在障碍的引用 id 中。)

    现在将文本限制在具有8dp 边距的屏障上。文本现在将随着障碍物浮动,障碍物始终位于准则和图像中较高者上方的8dp

    【讨论】:

    • 谢谢!障碍是要走的路。
    【解决方案2】:

    如何将 TextView 绑定到 Image,并简单地设置填充?图片总是屏幕宽,高度取决于屏幕类型 - 但文字总是比图片高 8dp。

        <TextView
                android:id="@+id/text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Keep me from overlapping"
                android:paddingBottom="8dp"
                app:layout_constraintBottom_toTopOf="@+id/guidelineGoldenRatio"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent" />
        
            <ImageView
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:srcCompat="@drawable/ic_supercat_popular" />
    

    【讨论】:

      【解决方案3】:

      根据 Cheticamp 的回答:

      <?xml version="1.0" encoding="utf-8"?>
      <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          xmlns:app="http://schemas.android.com/apk/res-auto">
      
          <androidx.constraintlayout.widget.Guideline
              android:id="@+id/guidelineGoldenRatio"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              app:layout_constraintGuide_percent="0.381966" />
      
          <androidx.constraintlayout.widget.Barrier
              android:id="@+id/barrier"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:barrierDirection="top"
              app:constraint_referenced_ids="imageView, guidelineGoldenRatio" />
      
          <TextView
              android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Keep me from overlapping"
              android:textSize="30dp"
              app:layout_constraintBottom_toTopOf="@+id/barrier"
              app:layout_constraintTop_toTopOf="@+id/barrier"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent" />
      
          <ImageView
              android:id="@+id/imageView"
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:adjustViewBounds="true"
              android:paddingTop="18dp"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:srcCompat="@drawable/ic_supercat_popular" />
      
      </androidx.constraintlayout.widget.ConstraintLayout>
      

      注意:文本和图像之间的最小距离进入图像填充。此填充还应补偿一半的文本高度(当围绕指南垂直居中文本时)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-22
        • 1970-01-01
        • 1970-01-01
        • 2019-08-19
        • 2018-08-30
        • 2021-08-21
        • 2021-12-26
        • 2016-11-04
        相关资源
        最近更新 更多