ConstraintLayout constraint layout implements negative layout

In the past, RelativeLayout could use layout_marginTop="-320dp" to achieve the effect you want, but ConstraintLayout can't, but we can use Space to achieve the same effect. Let's look at the
insert image description here
code below:

<?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">
 
 
    <android.support.v7.widget.AppCompatTextView
        android:id="@+id/tv_center"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="text"
        android:background="@color/gray"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
 
 
    <android.support.v7.widget.AppCompatTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingHorizontal="5dp"
        android:paddingVertical="2dp"
        android:text="最高"
        android:background="@color/clean_green"
        android:textColor="#FED9AB"
        app:layout_constraintBottom_toBottomOf="@+id/marginSpacer"
        app:layout_constraintStart_toStartOf="@+id/marginSpacer"
        app:layout_constraintTop_toTopOf="@+id/marginSpacer" />
 
    <!-- 相对底部 end 负10dp、 bottom 负 15dp -->
 
    <Space
        android:id="@+id/marginSpacer"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="5dp"
        app:layout_constraintBottom_toTopOf="@+id/tv_center"
        app:layout_constraintEnd_toEndOf="@+id/tv_center"
        app:layout_constraintTop_toTopOf="@+id/tv_center" />
 
</android.support.constraint.ConstraintLayout>

here. Negative layout can be realized.
END

Guess you like

Origin blog.csdn.net/lixinxiaos/article/details/123823669