ConstraintLayout记录
来源:互联网 发布:不履行网络安全法 编辑:程序博客网 时间:2024/06/05 11:55
一、概述
ConstraintLayout字面意思就是约束布局,约束布局可以说是RelativeLayout的进阶版,但性能比RelativeLayout更强大。在以往的布局里,主要是通过程序员编写XML的代码来实现,而约束布局则更偏向于用可视化的方式来编程。通过可视化的方式,更增加了布局的灵活性。
二、与其他布局的一些区别
- ConstraintLayout更偏向于可视化编程
ConstraintLayout可以通过在Design页面拖拽控件,然后,约束布局可以通过控件与控件之间,控件与其它参考线之间进行约束,这样更加容易实现界面的设置 - 可以减少布局的嵌套
约束布局只需要通过拖拽就可以实现所有控件的布置,可以减少在编程中各种布局的嵌套,加载的速度更加快。 - 界面的设置更加灵活
每一个控件都可以有多个方向设置约束,而且也可以用参考线,偏移度等来实现每一个控件的布置,这样可以使界面的设置更加灵活。
三、ConstraintLayout的基本内容
1. 相对定位
相对定位是在ConstraintLayout中创建布局的最基本构建块,也就是一个控件相对于另一个控件进行定位,可以从横向、纵向添加约束关系,这也是和RelativeLayout相似的地方,可以通过Left、Right、Start、End、Top、Bottom、Baseline这七个属性,来实现各控件之间的相对定位。
属性
layout_constraintLeft_toLeftOflayout_constraintLeft_toRightOflayout_constraintRight_toLeftOflayout_constraintRight_toRightOflayout_constraintTop_toTopOflayout_constraintTop_toBottomOflayout_constraintBottom_toTopOflayout_constraintBottom_toBottomOflayout_constraintBaseline_toBaselineOflayout_constraintStart_toEndOflayout_constraintStart_toStartOflayout_constraintEnd_toStartOflayout_constraintEnd_toEndOf
这些属性都要结合id值或parent才可以实现,如:
<Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="button1"app:layout_constraintRight_toLeftOf="button2"/>
这几行代码可以实现button1的右端向button2的左端约束
而:
<Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="button1"app:layout_constraintRight_toRightOf="button2"/>
这两段只是最后一行有差别,则表示button1和button2右对齐
2. Margins
这里可以表示成两个约束控件之间距离的最小值或者说是间隔,单位是dp
属性
android:layout_marginStartandroid:layout_marginEndandroid:layout_marginLeftandroid:layout_marginTopandroid:layout_marginRightandroid:layout_marginBottom
3. 偏向值
这也就是bias,当一对方向(左右,顶部底部其中一对)都添加约束时,就可以控制两边的比例值为0%~100%,表示约束的部件更偏向于哪一边越靠近0表示越靠近顶部或左边,100则反之
属性
app:layout_constraintVertical_bias=""app:layout_constraintHorizontal_bias=""
这里的“”里可以表示0~1的值,0即为0%,1即为100%
4. 尺寸约束
这里可以分为三个部分,第一个是设置最小的尺寸,即minWidth和minHeight(这个只有在wrap_content时有效);第二个是通过对约束条件的设置,这个其实在上面已经有提及,就是通过对两边的约束,达到大小对齐的状态
如:
<Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="button1"/><Buttonandroid:id="@+id/button2"android:layout_width="0dp"android:layout_height="wrap_content"android:text="button2"app:layout_constraintRight_toRightOf="@+id/button1"app:layout_constraintLeft_toLeftOf="@+id/button1"/>
这里可以实现button2的大小对齐button1,也可以设置间隔来使大小不同
第三个是设置宽高比例,顾名思义就是空间的宽和高的大小之比
属性
app:layout_constraintDimensionRatio="3:1"
即宽比高等于3比1,这里的layout_width和layout_height要有一个为0
如果两个相对的方向都有约束则要加上H和W来表示基准值
5. Guideline
这个相当于一个辅助线,分为水平和垂直两种,它可以帮助对于某些特定位置的约束,也就是说增加一条边,多一个约束的地方,使控件的约束更灵活
属性
layout_constraintGuide_beginlayout_constraintGuide_endlayout_constraintGuide_percent
上面前两个表示距离顶部或左边的距离和距离底部和右边的距离,以dp为单位,第三个这是表示宽高的百分比,以比例为单位可以填0~1
<android.support.constraint.Guideline android:id="@+id/guideline" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintGuide_percent="0.5"/>
这个代码表示增加一条垂直在中间的辅助线,控件也可以以这条边来增加约束
6.链条Chain
这一部分我看得不怎么熟,我自己理解就是建立一个双向的链接,把两个控件连成一个整体,如果水平建立了链接,那么就是能在垂直方向上移动,如果是垂直方向上建立链接,那就只能在水平方向上移动控件,如果两个方向都有链接,就不能直接移动,要通过修改其他数值才可以,例如Margins值,其中,最左边(或顶部)的控件是链条的头。链条的种类分为Spread Chain,Spread Inside Chain,Weighted Chain,Packed Chain,Packed Chain with Bias这几种
- ConstraintLayout记录
- [问题记录]Android | 初见ConstraintLayout
- ConstraintLayout
- ConstraintLayout
- ConstraintLayout
- ConstraintLayout
- ConstraintLayout
- ConstraintLayout
- constraintlayout
- constraintLayout
- ConstraintLayout
- 技术| Android ConstraintLayout初级使用记录
- Android ConstraintLayout
- 初试constraintLayout
- Android ConstraintLayout
- ConstraintLayout总结
- ConstraintLayout教程
- ConstraintLayout 简介
- Java循环变量在哪里赋值
- Ubuntu16安装opencv
- Java从键盘录入并写入到文件中
- android_内存卡播放器
- pmm-server服务部署解读
- ConstraintLayout记录
- 斐波那契数列的通项f(n)
- sql 的update
- Windows上搭建Kafka+zookeeper运行环境
- 简单计算器
- 静态方法和实例化方法的区别
- java笔记(二)
- Unity3D Standard Assets 简介之 Vehicles
- 算法系列——Plus One