ConstraintLayout是AndroidStudio2.2新增的一个功能,那么这个到底是什么呢?首先第一点我们知道传统的安卓开发,页面基本都是XML编写实现,特别在一些复杂的页面上需要嵌套多层,降低了页面加载的效率,因为ConstraintLayout就可以很好的优化布局,还有一点我们羡慕IOS的拖拽XML页面在这里也可以更好的实现。当然我所说的以上两点都是优化以前的布局,这也是Google极力要做的事情

开始

想要使用ConstraintLayout,首先AS版本必须升级到2.2以上(我基本都是逢新必更),首先需要在app/build.gradle文件中添加ConstraintLayout依赖

implementation 'com.android.support.constraint:constraint-layout:1.1.2'
复制代码

然后在项目的build.gradle文件buildscript和allprojects的repositories中添加google()

allprojects {
repositories {
google()
jcenter()
}
}
复制代码

然后同步就可以愉快的使用ConstraintLayout了~~

首先我们按照Google文档的顺序依次学习https://developer.android.com/reference/android/support/constraint/ConstraintLayout 先来一波api

1. layout_constraint[自身控件位置]_to[目标控件位置]Of=="[目标控件ID]"

  • 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

看到这些猜也能猜出个大概~比如layout_constraintLeft_toLeftOf就是说当前控件的Left在目标控件的Left上。如果目标控件为父控件则id可以直接写成parent。比如要实现B控件在A控件右面,则在B控件中设置layout_constraintLeft_toRightOf。意思就是说B控件的左面在A控件的右面~

2. Margins

  • android:layout_marginStart
  • android:layout_marginEnd
  • android:layout_marginLeft
  • android:layout_marginTop
  • android:layout_marginRight
  • android:layout_marginBottom

这个与之前其他viewgroup属性一致,不一样的就是多了以下几点:

  • layout_goneMarginStart
  • layout_goneMarginEnd
  • layout_goneMarginLeft
  • layout_goneMarginTop
  • layout_goneMarginRight
  • layout_goneMarginBottom

goneMargin属性是指目标控件GONE掉之后,自身控件可以设置个margin值,这里有一点需要敲黑板,目标控件就是相对于的那个控件

3. Bias

  • `layout_constraintHorizontal_bias``
  • layout_constraintVertical_bias

这个属性的意思是可以使用偏差属性调整定位以使一侧偏向另一侧,即控件距离左右百分比(layout_constraintHorizontal_bias)和距离上下百分比(layout_constraintVertical_bias)

4. WRAP_CONTENT : enforcing constraints (*Added in 1.1*)

强制约束

  • app:layout_constrainedWidth=”true|false”
  • app:layout_constrainedHeight=”true|false”

true代表防止约束失效,默认为false,比如:B在A的右边app:layout_constraintLeft_toRightOf="@+id/a",但是当A的内容越来越多并且超过了A到父控件最右的距离,此时就会约束失效使B的一部分出现了A的非右边。如果B设置了该属性为true,则B始终出现在A的右边,不会发生约束失效

5. Ratio

app:layout_constraintDimensionRatio="H,16:9"
复制代码

不用多说百分比布局是android中常用的一种适配布局H或W则代表以高或宽为基准

6. Guideline

layout_constraintGuide_begin 距离父容器起始位置的距离(左侧或顶部)

layout_constraintGuide_end 距离父容器结束位置的距离(右侧或底部)

layout_constraintGuide_percent 距离父容器宽度或高度的百分比

其实很好理解,比如

  <android.support.constraint.Guideline
android:id="@+id/guide1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" /> <android.support.v7.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@+id/guide1" />
复制代码

则表示在垂直方向上画一根基准线(只是参考线,并不进行view绘制)然后其他控件可以根据这条线进行放置

7. Barrier

<android.support.v7.widget.AppCompatButton
android:id="@+id/a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="a" /> <android.support.v7.widget.AppCompatButton
android:id="@+id/b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bbbbbbbbbbbbbbb"
app:layout_constraintTop_toBottomOf="@+id/a" /> <android.support.v7.widget.AppCompatButton
android:id="@+id/c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="c"
app:layout_constraintLeft_toRightOf="@+id/barrier" /> <android.support.constraint.Barrier
android:id="@+id/barrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="right"
app:constraint_referenced_ids="a,b" />
复制代码

显而易见,你可以把他看做一个容器constraint_referenced_ids=控件id,然后把这些控件看做一个整体

8. Group

它和Barrier有异曲同工之处,相同的都是你可以把他们看做一个容器,不同的是他是控制整个容器之中的所有的控件的可见或者不可见,比如android:visibility="gone",那它所包裹的左右控件都会gone。

当然ConstraintLayout的Api不止这些,需要我们真真切切的去使用它,你会发现它真的很好用~

参考

Android开发文档 [Developer][developer.android.com/reference/a…]

最新文章

  1. Maven下java.lang.NoClassDefFoundError
  2. Mac OS 中的 Python(和 NumPy)开发环境设置
  3. 菜鸟学习WCF笔记-概念
  4. Visual Studio 2013常用快捷键
  5. js自动刷新页面代码
  6. JQuery Datatables(二)
  7. T-SQL流程控制
  8. Unity cg vertex and fragment shaders(二)
  9. Tuple
  10. ubuntn安装五笔输入法
  11. 【翻译】Ext JS 4——Ajax和Rest代理处理服务器端一场和消息的方法
  12. 【转】RCP中org.eclipse.core.runtime.CoreException
  13. python正则下载图片
  14. 边缘触发(Edge Trigger)和条件触发(Level Trigger)
  15. 洛谷P3302 森林
  16. 2016-2017-2 20155309 南皓芯java第六周学习总结
  17. Android Support Library 23.2介绍(翻译自官方文档)
  18. oracle 执行的时候,显式输出结果!: set serveroutput on;
  19. Linux进程管理——fork()和写时复制
  20. 由UI刷新谈到线程安全和Android单线程模型

热门文章

  1. jenkins登录信息无效,忘记密码
  2. 1015 Reversible Primes (20 分)
  3. Spring (五):AOP
  4. TP基础
  5. 9.1 ArrayList(集合)的使用,与array(数组)的对比
  6. 配置spark历史服务(spark二)
  7. java中取得用户输入的方法
  8. CopyOnWriteArrayList线程安全的集合
  9. Java面向对象的总结
  10. webWMS开发过程记录(一)- 软件开发的流程