约束布局(constraintlayout) 最强大官方指定布局

一、属性
本以为百分比布局已经很无敌了,没想到约束布局尽然比他还勇猛。他就是AS2.3以后谷歌 布局亲儿子。
目前AS2.3以后自带的都是这个约束布局, 版本低的可以导入使用:
implementation ‘androidx.constraintlayout:constraintlayout:1.1.3’

  官网:https://developer.android.google.cn/reference/android/support/constraint/ConstraintLayout
<?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”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".initapp.WelcomeActivity">

</androidx.constraintlayout.widget.ConstraintLayout>

常用属性
layout_constraintLeft_toLeftOf
layout_constraintLeft_toRightOf
layout_constraintRight_toLeftOf
layout_constraintRight_toRightOf
layout_constraintTop_toTopOf
layout_constraintTop_toBottomOf
layout_constraintBottom_toTopOf
layout_constraintBottom_toBottomOf
layout_constraintBaseline_toBaselineOf
layout_constraintStart_toEndOf
layout_constraintStart_toStartOf
layout_constraintEnd_toStartOf
layout_constraintEnd_toEndOf

二、使用

<TextView
//宽高为0先不在这定义,一般应用要适应各种尺寸的手机, 所以做app时尽量不要定死,按比例来。
android:layout_width=“0dp”
android:layout_height=“0dp”

    //约束布局必须要有约束才会生效下面四个属性为约束,使控件居中
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    
    //在这给控件定义比列的宽高父容器的总宽高都为1
    app:layout_constraintWidth_percent="0.5"
    app:layout_constraintHeight_percent="0.5"
    
    //上面已经约束居中了,调整左右可随意调整位置
    app:layout_constraintHorizontal_bias="0.1" //水平距左距离,默认0.5居中
    app:layout_constraintVertical_bias="0.1" //竖直距上距离,默认0.5
    />

在这里插入图片描述
可以得到上图,一般情况用这几个属性就可以了,百分比布局用法,更够解决基本上的问题。

三、更多属性
1:在另一个控件上下左右边
app:layout_constraintRight_toLeftOf="@+id/text" //在text的左边
layout_constraintTop_toBottomOf 在控件的下边
layout_constraintBottom_toTopOf 在控件的上边
layout_constraintLeft_toRightOf 在控件的右边

2:在另一个控件的角度和距离
app:layout_constraintCircle="@+id/text"
app:layout_constraintCircleAngle=“90”(角度)
app:layout_constraintCircleRadius=“100dp”(两控件中心到中心的距离)

3:控件的宽高比设置大小
首先通过android:layout_width、android:layout_height直接指定大小。或者通过app:layout_constraintWidth_percent、app:layout_constraintHeight_percent指定百分比大小。
宽和高指定一个就行,然后使用以下属性
app:layout_constraintDimensionRatio=“H,1:3” (已指定宽或者高:未指定高或者宽)

4:权重weight(水平方向)
android:layout_width=“0dp”
app:layout_constraintHorizontal_weight=“2”

android:layout_width=“0dp”
app:layout_constraintHorizontal_weight=“1”

android:layout_width=“0dp”
app:layout_constraintHorizontal_weight=“1”

5:文字基准线对齐
app:layout_constraintBaseline_toBaselineOf="@id/text"

6:--------待跟新

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值