使视图顶部位于其他视图的终点之前ConstraintLayout

时间:2020-04-13 08:10:35

标签: android android-constraintlayout

我想实现一个用户界面,其中有两个 constraints

的视图 VIEW1 VIEW2
  1. VIEW1的高度为wrap_content
  2. VIEW2应该it's top以10dp出现在VIEW1结束之前。

Final UI i want with Code

4 个答案:

答案 0 :(得分:0)

enter image description here

您可以执行以下操作。

  • 顶视图(视图1),其约束条件为父视图的顶部,起点和终点。
  • 具有约束topToBottomOf =视图1的底部(视图2)。
  • 添加一个带有topToBottomOf =视图1且高度= 10dp或想要的重叠高度的圆角矩形(View 3)。

答案 1 :(得分:0)

您知道,设置layout_marginTop="-10dp"也不起作用。

您可以尝试将translationY="-10dp"设置为VIEW2,然后它将起作用。

答案 2 :(得分:0)

据我了解,您希望视图与添加的UI图像相同。可以使用准则来实现此UI。

enter image description here

    <?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>

答案 3 :(得分:0)

使用的另一种解决方案

一个固定高度的视图,该高度等于在VIEW1结束之前应开始多少dp VIEW2。

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

<?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>
相关问题