ConstraintLayout约束布局的属性盘点

学习文章:
1.ConstraintLayout约束布局的官方文档
2.郭霖-Android新特性介绍,ConstraintLayout完全解析
3. 鸿洋-ConstraintLayout 完全解析 快来优化你的布局吧
4.ConstraintLayout —— 约束布局 知识点整理
5.ConstraintLayout使用详解

  • 相对位置属性
    属性类似RelativeLayout

    		layout_constraintLeft_toLeftOf//目标view左边与另一个view左边对齐
             layout_constraintLeft_toRightOf//目标view左边与另一个view右边对齐
             layout_constraintRight_toLeftOf//目标view右边与另一个view左边对齐
             layout_constraintRight_toRightOf//目标view右边与另一个view右边对齐
             layout_constraintTop_toTopOf//目标view顶部与另一个view顶部对齐
             layout_constraintTop_toBottomOf//目标view顶部与另一个view底部对齐
             layout_constraintBottom_toTopOf//目标view底部与另一个view顶部对齐
             layout_constraintBottom_toBottomOf//目标view底部与另一个view底部对齐
             layout_constraintBaseline_toBaselineOf//基于baseline对齐
             layout_constraintStart_toEndOf//目标view起始边缘与另一个view结束边缘对齐
             layout_constraintStart_toStartOf//目标view起始边缘与另一个view起始边缘对齐
             layout_constraintEnd_toStartOf//目标view结束边缘与另一个view起始边缘对齐
             layout_constraintEnd_toEndOf//目标view结束边缘与另一个view结束边缘对齐
    

:Baseline是文字的基线,意思是基于控件中文字的下边缘对齐。

  • Margin边距
    两种Margin边距用法。
    第一种,和普通margin用法相同
	 android:layout_marginStart  
	android:layout_marginEnd
	android:layout_marginLeft
	android:layout_marginTop
	android:layout_marginRight
	android:layout_marginBottom

第二种,goneMargin,可以给当view被设置为View.Gone时设置边距,以避免UI布局混乱。

	layout_goneMarginStart
	layout_goneMarginEnd
	layout_goneMarginLeft
	layout_goneMarginTop
	layout_goneMarginRight
	layout_goneMarginBottom
  • BIOS(间隙比例)

    layout_constraintHorizontal_bias  
    layout_constraintVertical_bias  
    

    例如:layout_constraintHorizontal_bias =0.3,view水平方向左右间隙比例为3:7。
    有人会说我线性布局一行代码实现居中,你这也太太太啰嗦了,naive,下面才是约束布局的新特性,一行代码实现固定比例偏移。比如下面这个场景,我要让A左右两边的空档宽度比例为3:7。

    这里写图片描述

  • Guideline辅助线
    辅助线分为竖线和横线,用android:orientation来表示,可取”vertical””horizontal”.(可通过拖拽的方式来显示,通过下面的属性其中之一来确定其位置,多用percent)
    具体用法参考博客: 鸿洋-ConstraintLayout 完全解析 快来优化你的布局吧

	layout_constraintGuide_begin
	layout_constraintGuide_end
	layout_constraintGuide_percent
  • ConsTraintLayout权重比实现
    控件的宽高比:app:layout_constraintDimensionRatio (当控件的宽高中有一个为0dp的时才起作用),如app:layout_constraintDimensionRatio=“16:9”控件的宽高比例为16:9。
    app:layout_constraintHorizontal_weight //控件在水平方向占父布局的比例
	app:layout_constraintVertical_weight//控件在竖直方向占父布局的比例
  • 链Chain
    当多个控件通过相互约束发生联系的时候,就组成了一个链Chain,我们可以通过链头来设置链的类型chainStyle,第一个就是链头。
    总共有三个属性:
    spread,packed,spread_inside默认为spread。具体效果可参考: 鸿洋-ConstraintLayout 完全解析 快来优化你的布局吧
	app:layout_constraintHorizontal_chainStyle
	app:layout_constraintVertical_chainStyle
  • Group(群组)
    使用群组,可以将某些view分组在一起。集中起来进行控制。例如:控制布局中某几个view的可见性,就只需要设置group的可见性即可,他是通过ids将控件放到一个群组里的。
	<android.support.constraint.Group 
	android:id="@+id/id_group" 
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content" 
	app:constraint_referenced_ids="id_btn,id_tv" /> 
  • Barrier (屏障)
    这是一个辅助类,和Guidline一样不参与布局的绘制,是不可见的。
    Barrier 是用多个 View 作为限制源来决定自身位置的一种辅助线,它有两个比较重要的属性:
    1、barrierDirection,取值有top、bottom、left、right、start、end,用于控制 Barrier 相对于给定的 View 的位置。比如在上面的栗子中,Barrier 应该在 Title 的右侧,因此这里取值right(也可end,自行琢磨)。
    2、constraint_referenced_ids,取值是要依赖的控件的id(不需要@+id/)。Barrier 将会使用ids中最大的一个的宽(高)作为自己的位置。
    在这里插入图片描述
    参考博客文章:ConstraintLayout 中 Barrier 的使用

猜你喜欢

转载自blog.csdn.net/qq_33241516/article/details/84585231