Android新布局方式ConstraintLayout快速入门教程

 更新时间:2017年05月26日 11:19:32   作者:Android傳習錄  
谷歌在2016年的IO大会上推出的一种新的布局方式—-ConstraintLayout,这局是一种约束型的布局方式,下面这篇文章主要给大家介绍了Android中新布局方式ConstraintLayout的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。

前言

在Android开发中,我们通常是手写布局,很少会用拖动来写布局,虽然ConstraintLayout在I/O上以拖动来展现了各种功能,我估计在以后开发中,程序员还是习惯手撸代码。

我自己试着拖着用了一下,用得不是很明白 ,而且用起来效果不是很好。

那么

直接上手撸了一下~~~

其实很简单

Button1:app:layout_constraintBottom_toTopOf="@id/iv_head"

我们把这个属性拆开来看,constraintBottom指的本身的底部,即Button1的顶部,toTopOf是指ImageView的顶部,那么这句话的意思就是

Aligns the bottom of the desired view to the top of another.(官方原文)

翻译一下就是Button1的底部要和ImageView的顶部对齐

Button1 app:layout_constraintRight_toLeftOf="@id/iv_head"

根据上面的规则我们就知道Button1的右边要和ImageView的左边对齐。

其实很简单就是说两个View的某个方位要对齐

没了,就这么简单,其它属性可以举一反三,它比RelativeLayout控制起来更加得以就手。

  • layout_constraintTop_toTopOf — Align the top of the desired view to the top of another.
  • layout_constraintTop_toBottomOf — Align the top of the desired view to the bottom of another.
  • layout_constraintBottom_toTopOf — Align the bottom of the desired view to the top of another.
  • layout_constraintBottom_toBottomOf — Align the bottom of the desired view to the bottom of another.
  • layout_constraintLeft_toTopOf — Align the left of the desired view to the top of another.
  • layout_constraintLeft_toBottomOf — Align the left of the desired view to the bottom of another.
  • layout_constraintLeft_toLeftOf — Align the left of the desired view to the left of another.
  • layout_constraintLeft_toRightOf — Align the left of the desired view to the right of another.
  • layout_constraintRight_toTopOf — Align the right of the desired view to the top of another.
  • layout_constraintRight_toBottomOf — Align the right of the desired view to the bottom of another.
  • layout_constraintRight_toLeftOf — Align the right of the desired view to the left of another.
  • layout_constraintRight_toRightOf — Align the right of the desired view to the right of another.
  • If desired, attributes supporting start and end are also available in place of left and right alignment.

到此,你已经掌握了一大半的ConstraintLayout知识点

还有其它的一些属性

app:layout_constraintStart_toEndOf

意思就是Button的开始部分(从左往右看,开始部分就是Button的左边)与ImageView的右边是对齐的。

app:layout_constraintStart_toStartOf

这个就是说Button的左边与ImageView的左边是对齐的

不知道为什么上面已经出的属性能够满足布局需要了,为什么还要再出start和end的。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

相关文章

  • JetpackCompose Navigation导航实现流程

    JetpackCompose Navigation导航实现流程

    Navigation是Jetpack用于Android导航的组件,作用是处理页面跳转,以及页面跳转过程中的交互。使用Navigation,你就需要为每个页面设定一条唯一路径,它是一个String常量,形式是DeepLink的样子,从一个页面跳转到另一个页面,它通过输入目的地的路径进行转跳
    2023-01-01
  • Android仿腾讯QQ实现滑动删除 附源码下载

    Android仿腾讯QQ实现滑动删除 附源码下载

    仿腾讯QQ滑动删除操作,这篇文章主要为大家详细介绍了ListView滑动删除的具体操作方法,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • Android桌面插件App Widget用法分析

    Android桌面插件App Widget用法分析

    这篇文章主要介绍了Android桌面插件App Widget用法,结合实例形式分析了桌面插件App Widget的功能、布局、创建及使用方法,需要的朋友可以参考下
    2016-08-08
  • Android实现屏幕旋转四个方向准确监听

    Android实现屏幕旋转四个方向准确监听

    这篇文章主要为大家详细介绍了Android实现屏幕旋转四个方向准确监听,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-07-07
  • Android 异步任务和消息机制面试题分析

    Android 异步任务和消息机制面试题分析

    这篇文章主要为大家介绍了Android 异步任务和消息机制面试题分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • Android实现长图展开与收起效果

    Android实现长图展开与收起效果

    这篇文章主要为大家详细介绍了Android实现长图展开与收起效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-09-09
  • Android 使用版本控制工具时添加忽略文件的方式(详解)

    Android 使用版本控制工具时添加忽略文件的方式(详解)

    下面小编就为大家带来一篇Android 使用版本控制工具时添加忽略文件的方式(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-01-01
  • Android Studio中通过CMake使用NDK并编译自定义库和添加预编译库

    Android Studio中通过CMake使用NDK并编译自定义库和添加预编译库

    这篇文章是基于Android Studio 3.01版本的,NDK是R16。本文重点给大家介绍Android Studio中通过CMake使用NDK并编译自定义库和添加预编译库的相关知识,感兴趣的朋友一起看看吧
    2018-01-01
  • Android编程创建与解析xml的常用方法详解

    Android编程创建与解析xml的常用方法详解

    这篇文章主要介绍了Android编程创建与解析xml的常用方法,结合具体实例形式较为详细的分析了Android操作xml文件的步骤、实现技巧与相关注意事项,需要的朋友可以参考下
    2017-05-05
  • Android实现圆圈倒计时

    Android实现圆圈倒计时

    这篇文章主要为大家详细介绍了Android实现圆圈倒计时,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08

最新评论