Android开发之ConstraintLayout(约束布局)一个控件位于一个控件右上角类似RelativeLayout实现效果

老套路看下效果图:(绝对的原创!!!!)

实际上很简单,四个点对应上就行了,看代码:

<?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:gravity="center"
        android:id="@+id/sign_in_button"
        android:layout_width="280dp"
        android:layout_height="75dp"
        android:layout_marginTop="50dp"
        android:layout_marginBottom="75dp"
        android:text="测试数据"
        android:textSize="22sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="14dp"
        app:layout_constraintBottom_toTopOf="@+id/sign_in_button"
        app:layout_constraintEnd_toEndOf="@+id/sign_in_button"
        app:layout_constraintStart_toEndOf="@+id/sign_in_button"
        app:layout_constraintTop_toTopOf="@+id/sign_in_button" />
</androidx.constraintlayout.widget.ConstraintLayout>

直接拿去用吧,自己开始也不知道怎么布局,然后自己随意按照自己的思路将4个点连接就成功了。

 

 

升级版,研究了下左上右下四个角的摆放位置如下图:

 

直接上代码吧:

<?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/sign_in_button"
        android:layout_width="280dp"
        android:layout_height="75dp"
        android:layout_marginTop="50dp"
        android:layout_marginBottom="75dp"
        android:gravity="center"
        android:text="测试数据"
        android:textSize="22sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="14dp"
        app:layout_constraintBottom_toTopOf="@+id/sign_in_button"
        app:layout_constraintEnd_toEndOf="@+id/sign_in_button"
        app:layout_constraintStart_toEndOf="@+id/sign_in_button"
        app:layout_constraintTop_toTopOf="@+id/sign_in_button" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="@dimen/dp_50"
        android:layout_height="@dimen/dp_50"
        android:background="@android:color/holo_blue_light"
        app:layout_constraintBottom_toTopOf="@+id/sign_in_button"
        app:layout_constraintEnd_toStartOf="@+id/sign_in_button"
        app:layout_constraintStart_toStartOf="@+id/sign_in_button"
        app:layout_constraintTop_toTopOf="@+id/sign_in_button" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="@dimen/dp_50"
        android:layout_height="@dimen/dp_50"
        android:background="@android:color/holo_orange_dark"
        app:layout_constraintBottom_toBottomOf="@+id/sign_in_button"
        app:layout_constraintEnd_toStartOf="@+id/sign_in_button"
        app:layout_constraintStart_toStartOf="@+id/sign_in_button"
        app:layout_constraintTop_toBottomOf="@+id/sign_in_button" />

    <TextView
        android:layout_width="@dimen/dp_50"
        android:layout_height="@dimen/dp_50"
        android:background="@android:color/holo_purple"
        app:layout_constraintBottom_toBottomOf="@+id/sign_in_button"
        app:layout_constraintEnd_toEndOf="@+id/sign_in_button"
        app:layout_constraintStart_toEndOf="@+id/sign_in_button"
        app:layout_constraintTop_toBottomOf="@+id/sign_in_button" />


    <TextView
        android:id="@+id/textView"
        android:layout_width="300dp"
        android:layout_height="75dp"
        android:gravity="center"
        android:text="中央布局"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.3" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="@dimen/dp_50"
        android:layout_height="@dimen/dp_50"
        android:background="@android:color/holo_green_dark"
        app:layout_constraintBottom_toBottomOf="@+id/textView"
        app:layout_constraintEnd_toStartOf="@+id/textView"
        app:layout_constraintStart_toStartOf="@+id/textView"
        app:layout_constraintTop_toTopOf="@+id/textView" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="@dimen/dp_50"
        android:layout_height="@dimen/dp_50"
        android:background="@android:color/holo_red_light"
        app:layout_constraintBottom_toBottomOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="@+id/textView"
        app:layout_constraintStart_toEndOf="@+id/textView"
        app:layout_constraintTop_toTopOf="@+id/textView" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="@dimen/dp_50"
        android:layout_height="@dimen/dp_50"
        android:background="@android:color/holo_green_dark"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toStartOf="@+id/textView"
        app:layout_constraintStart_toEndOf="@+id/textView"
        app:layout_constraintTop_toTopOf="@+id/textView" />

    <TextView
        android:layout_width="@dimen/dp_50"
        android:layout_height="@dimen/dp_50"
        android:background="@android:color/holo_red_light"
        app:layout_constraintBottom_toBottomOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="@+id/textView"
        app:layout_constraintStart_toStartOf="@+id/textView"
        app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>

 

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页