ConstraintLayout你可能要了解的一些内容

2,660 阅读9分钟

好久没有写文章了!以至于我都不知道该写什么了?最近项目中对ConstraintLayout的使用明显增多了!所以今天和大家分享一下关于ConstraintLayout的一些你应该了解的内容!

本文知识点

  • ConstraintLayout的一些相应的属性及展示;
    1. 约束的相关属性
    2. 边距相关的属性
    3. 位置相关的属性
    4. 链相关的属性
    5. 辅助线相关的属性
    6. 一些不知怎么归类的属性
  • ConstraintLayout布局的时候应该注意的一些内容;
    1. 关于ID的问题
    2. 关于设置"0dp"的问题
    3. 关于屏幕适配的问题

1.ConstraintLayout的一些相应的属性及展示

关于这个控件我想大家应该都见过,只不过是你没有注意过吧!如果你的Android Studio的版本在3.0以上的话,那么跟布局一般都是ConstraintLayout,以前一般的做法都是,直接替换成相应的布局,直到有一天,我突然发现这个控件如此之强大,强大到我都不再想用其它控件了!但是强烈建议你们使用代码去写!不要使用拖拽的那种方式,因为我觉得每次拖拽的时候我的电脑卡的不行!这个理由是不是很牵强~~~哈哈!

关于ConstraintLayout属性,我准备分成几类进行讲解

  • 1.约束的相关属性
  • 2.边距相关的属性
  • 3.位置相关的属性
  • 4.链相关的属性
  • 5.辅助线相关的属性
  • 6.一些不知怎么归类的属性

1.1 约束的相关属性

  • 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

这里和你说一个简单方便记忆的方法,XXX_toXXXOf前面的是你现在的控件,后面的XXX是你要依赖的控件,拿layout_constraintLeft_toLeftOf说明一下:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1按钮1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <Button
        app:layout_constraintTop_toBottomOf="@id/btn1"
        app:layout_constraintLeft_toLeftOf="@id/btn1"
        app:layout_constraintRight_toRightOf="@id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮2" />

</android.support.constraint.ConstraintLayout>

效果图

  1. 先主要看按钮1的约束条件,按钮1的左边以父控件的左边为约束条件,右边以父控件的右边为约束条件,这个时候,会有相应的居中显示(也就是说,如果你左右都设置了约束条件的话,那么如果有相应的空间,那么它会居中显示)!

  2. 在看按钮2的约束条件,按钮2的左边以按钮1的左边为约束条件,按钮2的右边以按钮1的右边为约束条件,这里注意一点,如果两个控件不一样大的时候,会以中心线对齐!

基本上就这么多,没有什么好说的。这里建议大家使用看一下效果,使用多了自然就知道效果了!

1.2 边距相关的属性

  • android:layout_marginStart
  • android:layout_marginEnd
  • android:layout_marginLeft
  • android:layout_marginTop
  • android:layout_marginRight
  • android:layout_marginBottom
  • layout_goneMarginStart
  • layout_goneMarginEnd
  • layout_goneMarginLeft
  • layout_goneMarginTop
  • layout_goneMarginRight
  • layout_goneMarginBottom

这里的边距和之前的边距是一样的,但是layout_goneMarginXXX这个属性是当位置约束目标的可见性为View.GONE,您还可以使用以下属性指示要使用的不同边距值。这里说明一下:如果你约束的控件GONE了之后,是以左上开始计算距离的,剩下的试一下你就知道了!

1.3 位置相关的属性

首先我真的不知道,这么分这些属性是否正确,所以只要看相关的属性就可以了!

1.3.1 百分比的属性

  • layout_constraintHorizontal_bias 水平的百分比
  • layout_constraintVertical_bias 垂直的百分比
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1按钮1"
        app:layout_constraintHorizontal_bias="0.3"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>

效果图

这里的layout_constraintXXX_bias相当于一个百分比也就是距离的百分比,像上面的的就是距离左边30%那么右边的就是70%了!

  • app:layout_constraintDimensionRatio="1:1" 设置相应的宽高比
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:text="按钮1"
        app:layout_constrainedWidth="true"
        app:layout_constraintDimensionRatio="1:1" />
</android.support.constraint.ConstraintLayout>

效果图

上面是宽高比是"1:1",这里默认是宽高比的!

如果你像设置高度比这样写就可以了**"h,2:1"**但是","别千万别忘了

效果图

1.3.2 圆角半径的一些属性

  • layout_constraintCircle :引用另一个小部件ID
  • layout_constraintCircleRadius :到其他小部件中心的距离
  • layout_constraintCircleAngle :小部件应该处于哪个角度(以度为单位,从0到360)
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮2"
        app:layout_constraintCircle="@id/btn1"
        app:layout_constraintCircleAngle="135"
        app:layout_constraintCircleRadius="100dp" />
</android.support.constraint.ConstraintLayout>

效果图

这里为了更好的说明,所以我准备在用两张图说明一下:

首先,角度是逆时针计算的,半径是以图形的中心进行计算的!剩下的你写一遍基本上就知道了!!!

1.3.3 限制的一些属性

  • android:minWidth 设置布局的最小宽度
  • android:minHeight 设置布局的最小高度
  • android:maxWidth 设置布局的最大宽度
  • android:maxHeight 设置布局的最大高度

这些属性没有什么说的,和之前一样!

1.4 链相关的属性

关于链表的属性很有意思,我个人理解他是LinearLayout中weight的加强版!我们先说说简单的实现吧!为什么叫做链呢?因为所有的同方向的约束组成了一个链式结构!

  • layout_constraintHorizontal_chainStyle
  • layout_constraintVertical_chainStyle
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1"
        app:layout_constrainedWidth="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/btn2" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮2"
        app:layout_constrainedWidth="true"
        app:layout_constraintLeft_toRightOf="@id/btn1"
        app:layout_constraintRight_toLeftOf="@id/btn3" />

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮3"
        app:layout_constrainedWidth="true"
        app:layout_constraintLeft_toRightOf="@id/btn2"
        app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>

效果图

这里要注意一个相应的问题,怎么理解上面我说的链式结构呢?仔细看的童鞋,在上面这个例子中可能会发现,每一个控件左右的都有相应的约束,所以才叫链表式的结构!

效果图
这张图完全能说明我上面说的内容!

  • layout_constraintHorizontal_weight
  • layout_constraintVertical_weight

这里既然说到LinearLayout就在这里说一下这两个属性吧!因为这两个属性和weight属性差不多,基本上的用法是一样的

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="按钮1"
        app:layout_constrainedWidth="true"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/btn2" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="按钮2"
        app:layout_constrainedWidth="true"
        app:layout_constraintHorizontal_weight="2"
        app:layout_constraintLeft_toRightOf="@id/btn1"
        app:layout_constraintRight_toLeftOf="@id/btn3" />

    <Button
        android:id="@+id/btn3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="按钮3"
        app:layout_constrainedWidth="true"
        app:layout_constraintHorizontal_weight="3"
        app:layout_constraintLeft_toRightOf="@id/btn2"
        app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>

这里就是设置了相应的一些weight的属性!效果图就变成了这个样子!!!

效果图

在说说上面的两个属性,关于这两个属性都差不多,只是一个是约束水平的一个是约束垂直的,还是用一张图说明一下,看一眼效果你就知道了!!!

效果图

这张效果图,说明了chainStyle可以设置的一些属性!关于链式结构基本上注意的内容就这么多!!!也都是平时项目上能用到的!但是千万要注意一点,必须左右都有相应的约束!否则不会有这种效果

"三遍" 重要的事情说"三遍" 哈哈!!!

1.5 辅助线相关的属性

在ConstraintLayout中有一个辅助线的概念,它是不会显示在页面上的,只是一条你假象的线,然后你可以以此为约束进行布局!

  • android:orientation 辅助线的方向
  • app:layout_constraintGuide_begin 距离顶部的距离
  • app:layout_constraintGuide_end 距离底部的距离
  • app:layout_constraintGuide_percent 相当于父容器的百分比
<android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="100dp" />

效果图

可以看出,Guideline计算的距离都是依靠父容器的距离进行约束的,所以这里你即使设置了相应的控件约束,其实是不好使的!我试过,但是如果你真的像以控件进行约束也不是不可以的,我的做法是:直接把控件的大小写出来,这样不用上面的三个距离的尺寸就可以进行相应的布局了,虽然这样有点傻!剩下两个你试一下就可以了!图就不往上贴了!!!

1.6 一些不知怎么归类的属性

  • layout_constraintBaseline_toBaselineOf 基线对其
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:text="按钮1" />

    <Button
        android:id="@+id/btn2"
        app:layout_constraintBaseline_toBaselineOf="@id/btn1"
        app:layout_constraintLeft_toRightOf="@id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:text="按钮2" />
</android.support.constraint.ConstraintLayout>

效果图

  • layout_constraintWidth_percent
  • layout_constraintHeight_percent

宽度和高度占父控件的百分比,其中的值是0~1的!

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintWidth_percent="0.5"
        app:layout_constraintHeight_percent="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:text="按钮1" />
    
</android.support.constraint.ConstraintLayout>

效果图

其实就相当于占用父容器的百分比,上面就相当于50%

2. ConstraintLayout布局的时候应该注意的一些内容

2.1 关于ID的问题

其实我也不是到是Android Studio还是SDK的版本影响,但是有的时候在使用链式结构的时候,会出现控件找不到的现象?为什么呢?因为在使用链式结构的时候,前面往往使用后面的控件ID,但是有的时候就不会出现这种状况,后来,我找到了一种解决方法,就是在前面引用的时候这样写"@+id/xxx",乍一看没有什么,但是其实多了一个"+"号,这个"+"号的作用是在没有发现这个控件ID的时候,主动创建一个相应的ID!就酱紫。。。

2.2 关于设置"0dp"的问题

其实在不涉及到左右或者上下比例的情况下,只要你在相应方向都有约束的情况,那么相应的方向上设置"0dp"的时候,它会直接铺满全屏的!但是如果涉及到左右或者上下比例的情况下,就需要设置相应的layout_constraintXXX_weight了!

2.3 关于屏幕适配的问题

都知道Android屏幕适配真的是一件非常头疼的事情,其实这个控件能帮我们解决相当大的一部分问题;一般我的做法是使用layout_constraintXXX_percent直接把控件按照百分比进行相应的计算!能解决一部分的适配问题,但是内部的自己没有办法解决,需要你自己去解决!!!!

基本上知道了上面这些,对于这个控件的使用就可以非常轻松了!!!不知道还有没有什么落下的,如果有落下的我想起来在进行补充,如果你有什么问题也可以给我留言,我看到了一定及时回复你!!!