使视图顶部位于其他视图 ConstraintLayout 结束之前

Make top of view to be before end of other view ConstraintLayout

我想实现 A UI 我有两个视图 VIEW1VIEW2 约束条件

  1. VIEW1 的高度为 wrap_content
  2. VIEW2 应该 it's top 在 VIEW1 结束之前,例如 10dp。

Final UI i want with Code

你可以这样做。

  • 顶视图(视图 1),具有父项的顶部、开始和结束限制。
  • 具有约束的底部(视图 2)topToBottomOf = 视图 1。
  • 添加一个圆角矩形(视图 3),其中 topToBottomOf = 视图 1 和高度 = 10dp 或任何您想要的重叠高度。

如您所知,设置 layout_marginTop="-10dp" 也不行。

您可以尝试将 translationY="-10dp" 设置为 VIEW2,这样就可以了。

As I understand you want view same as you added UI Image. This UI can be achieved using Guideline.

    <?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textview9"
        android:layout_width="0dp"
        android:layout_height="@dimen/_100sdp"
        android:background="@color/bg_counter"
        android:text="textview"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

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

    <TextView
        android:id="@+id/textview10"
        android:layout_width="@dimen/_80sdp"
        android:layout_height="0dp"
        android:background="@color/colorAccent"
        android:text="textview"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/guideline" />

</androidx.constraintlayout.widget.ConstraintLayout>

另一种解决方案是使用

a view with fixed height that equal to how many dp VIEW2 should start before end of VIEW1.

完整代码(也有相同的结束角到底部)::

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/mainContent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@color/faded_orange"
        android:text="TextView"
        android:translationY="0dp"
        android:textAlignment="center"
        app:layout_constraintTop_toTopOf="@id/space"
        app:layout_constraintBottom_toBottomOf="@id/space2"
        app:layout_constraintStart_toStartOf="@id/space" />

    <Space
        android:id="@+id/space"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="@id/topLayout"
        />

    <Space
        android:id="@+id/space2"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/bottomLayout"
        />

    <TextView
        android:id="@+id/topLayout"
        android:layout_width="0dp"
        android:layout_height="@dimen/_100sdp"
        android:background="@drawable/header_bg"
        android:text="TextView"
        android:textAlignment="center"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/bottomLayout"
        android:layout_width="0dp"
        android:layout_height="@dimen/_100sdp"
        android:background="@drawable/bg_bottom_view"
        android:text="TextView"
        android:textAlignment="center"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>