ConstraintLayout其实是一个升级版的RelativeLayout

ConstraintLayout 布局属性主要有位置控制,边界间距,水平对齐,偏移属性,设置宽高比,大小控制等

一、位置控制

app:layout_constraintLeft_toLeftOf  //我最左边的位置 在别人的左边,以下类似app:layout_constraintLeft_toRightOfapp:layout_constraintRight_toLeftOfapp:layout_constraintRight_toRightOfapp:layout_constraintTop_toTopOfapp:layout_constraintTop_toBottomOfapp:layout_constraintBottom_toTopOfapp:layout_constraintBottom_toBottomOfapp:layout_constraintStart_toEndOfapp:layout_constraintStart_toStartOfapp:layout_constraintEnd_toStartOfapp:layout_constraintEnd_toStartOf

比如app:layout_constraintLeft_toLeftOf="parent"表示和父布局的左边对齐,也可以指定为@id/button1,就是和button1左对齐

二、边界间距

android:layout_margin=""    //表示四周边界为多少android:layout_marginTop="" //表示距顶部边界为多少android:layout_marginBottom=""android:layout_marginLeft=""android:layout_marginRight=""android:layout_marginStart=""android:layout_marginEnd=""

注意比如要举例父布局顶部10dp,首先要声明和父布局顶端对齐app:layout_constraintTop_toTopOf="parent",之后再声明边距 android:layout_marginTop="10dp"

三、水平对齐

app:layout_constraintBaseline_toBaselineOf="@id/main_btn_expendView1"

针对上面三点举个例子:

之后如果你想让GroupView2按钮充满ExpendView1的左侧,就不能使用warp_content/固定值等,因为我们为控件添加的约束都是约束“Constraint”,这个约束有点像橡皮筋一样会拉这个控件,但是并不会改变控件的尺寸,所以这时候应该将GroupView2的宽度设置为 ‘0’(android:layout_width="0dp"


四、偏移属性

 layout_constraintHorizontal_bias    (水平方向偏移)(范围0-1) layout_constraintVertical_bias      (垂直方向偏移)(范围0-1)  

不过这两个属性要配合以下等约束使用 

app:layout_constraintLeft_toLeftOf=”parent” app:layout_constraintRight_toRightOf=”parent” app:layout_constraintTop_toTopOf=”parent” app:layout_constraintBottom_toBottomOf=”parent”

即如果在竖直方向偏移,就要设置该控件的上下边界,此偏移属性才会生效

比如新增一个按钮,在GroupView2下方,同时水平向右偏移0.05

如果只指定上下边界

app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"

则会在中间


此时在加入偏移属性app:layout_constraintHorizontal_bias="0.05"


五、设置宽高比

app:layout_constraintDimensionRatio 

注意:需要把宽(layout_width)或者高(layout_height)其中的一个设置为0dp

举个例子:

其中:app:layout_constraintDimensionRatio="W,2:1",也可以改为app:layout_constraintDimensionRatio="2:1"

默认是W:H


当然也可以指定为H:W,如下

app:layout_constraintDimensionRatio="H,2:1"


六、大小控制

layout_constraintHorizontal_weight //水平方向上比重,类似线性布局layout_constraintVertical_weight   //垂直方向上比重,类似线性布局
1. 实现水平方向线性布局, 所有View都必须设置左右边界控制属性,而且相互控制  2. 实现竖直方向线性布局, 所有View都必须设置上下边界控制属性,而且相互控制  2. 实现比重大小控制,必须设置 layout_width=”0dp”

举例:

设置横向三个Button,比例为2:1:2


此时这三个Button成为一条链,在这个链的最左侧的元素成为链头,我们可以在链头设置风格

app:layout_constraintHorizontal_chainStyle="spread_inside"app:layout_constraintVertical_chainStyle=""

spread                元素将被展开(默认样式)

spread_inside      类似,但链的端点将不会扩展

packed               链的元素将被打包在一起。 孩子的水平或垂直偏差属性将影响包装元素的定位

更多相关文章

  1. 在Androd studio 中,TextView设置Text居然都是大写
  2. Android(安卓)动画系统汇总
  3. Android(安卓)N 7.x起 APP分屏模式 问题总结
  4. Android中Paint字体属性的一些设置
  5. Android动画分类
  6. Android(安卓)Material Design TabLayout属性app:tabMode和app:
  7. Android(安卓)Parcelable对象生成:ParcelableGenerator
  8. Android布局属性集合
  9. 浅析Android中的visibility属性

随机推荐

  1. 第六周Android学习笔记
  2. Unity Android交互过坑指南
  3. Android SDK下载慢的最给力解决办法
  4. Android项目结构和AndroidManifest.xml
  5. Android 之 ContentProvider的简介-相关
  6. android仿美团底部导航栏的点击效果——
  7. 【Android】Android Studio 1.5+ 中混合
  8. Android 应用程序中使用 Internet 数据
  9. Android 用SlidingDrawer实现抽屉效果
  10. android小复习