Android:随笔—— ConstraintLayout 实战经验
这篇文章呢我们谈一谈,约束布局常用的一些需求和坑
一、如果我想让一个控件在父布局里左右填充怎么办(上下填充,上下左右都填充同理)
- match_parent 方式
<View android:id="@+id/a" android:layout_width="match_parent" android:layout_height="60dp" android:background="@android:color/holo_red_light" />
- 相对布局的方式(
android:layout_width="0dp"
是关键)
<View android:id="@+id/a" android:layout_width="0dp" android:layout_height="60dp" android:background="@android:color/holo_red_light" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" />
ConstraintLayout 没有 match_parent 所以推荐使用第二种方式,虽然看效果是没啥事,但是官方的坑我表示不踩
二、替代百分比布局
有时候想把控件按照比例填充父布局,Android 屏幕适配这么复杂,好像不容易实现
这时候约束布局有两个属性 layout_constraintWidth_percent layout_constraintHeight_percent
这岂不是把一直不温不火的百分比布局给革命了
<View android:id="@+id/a" android:layout_width="0dp" android:layout_height="50dp" android:background="@android:color/holo_red_light" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintWidth_percent="0.3" />
我只放这么一段代码大家应该就知道怎么用了,我也就不多了说了。
默认是居中的如果想调整位置请结合
app:layout_constraintHorizontal_bias="0.3"
食用
三、控件宽度一定,我想左右居中显示怎么办(上下居中同理)
<View android:id="@+id/a" android:layout_width="120dp" android:layout_height="60dp" android:background="@android:color/holo_red_light" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" />
与左右填充相似,只不过宽度固定了而已,两条绳子拉着一个控件居中
四、控件宽度一定,左右按百分比显示怎么办,我就是不想居中(上下同理)
<View android:id="@+id/a" android:layout_width="120dp" android:layout_height="60dp" android:background="@android:color/holo_red_light" app:layout_constraintHorizontal_bias="0.3" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" />
app:layout_constraintHorizontal_bias="0.3"
一个属性就搞定了
上下使用
app:layout_constraintVertical_bias
来实现
五、我想两个控件左右平分空间,对没错就是抢 LinearLayout 的饭碗(上下居中同理)
<View android:id="@+id/a" android:layout_width="0dp" android:layout_height="60dp" android:background="@android:color/holo_red_light" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@+id/b" /> <View android:id="@+id/b" android:layout_width="0dp" android:layout_height="60dp" android:background="@android:color/holo_blue_light" app:layout_constraintLeft_toRightOf="@+id/a" app:layout_constraintRight_toRightOf="parent" />
因为有个约束链的存在所以这个稍微有点复杂,先来一张约束示意图看一下,约束链下边在讲
六、我想两个控件左右按比例平分空间,平分多没有意思啊(上下同理)
<View android:id="@+id/a" android:layout_width="0dp" android:layout_height="60dp" android:background="@android:color/holo_red_light" app:layout_constraintHorizontal_weight="1" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@+id/b" /> <View android:id="@+id/b" android:layout_width="0dp" android:layout_height="60dp" android:background="@android:color/holo_blue_light" app:layout_constraintHorizontal_weight="2" app:layout_constraintLeft_toRightOf="@+id/a" app:layout_constraintRight_toRightOf="parent" />
上面的例子是 1:2 看写法是不是和 LinearLayout 的权重写法简直一毛一样
上下权重设置
app:layout_constraintVertical_weight=""
一定记得android:layout_width="0dp"
是 0dp
坑点
宽高在需要自动控制长度的时候一定是设置为 0dp,不然没什么效果,甚至达不到预期,我也推荐约束布局中只是用 0dp 、wrap_content 、固定的值。切记 match_parent 是没啥卵用,虽然看着有效果,但是我也不敢碰,说多了都是泪。
切记只有约束过的边设置 margin 才能看出效果,注意我说的是能看出,虽然设置了 margin 也是有意义的但是看不出来
平时我们摞控件的时候一定记得处理右边和底部的约束,好多人控件显示不全就是这个问题

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Toolbar的简单使用
Toolbar(android.support.v7.widget.Toolbar)是Android5.0之后出现的用来取代ActionBar的一个控件。 Toolbar的使用方法 ●最简单的使用 xml布局 <android.support.v7.widget.Toolbar android:layout_width="match_parent" android:id="@+id/toolbar" app:popupTheme="@style/toolbarMenuStyle" android:layout_height="?attr/actionBarSize"> </android.support.v7.widget.Toolbar> 除了在布局文件中加入Toolbar外还需要修改values/styles.xml文件 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <...
- 下一篇
《React Native 精解与实战》书籍连载「React Native 底层原理」
此文是我的出版书籍《React Native 精解与实战》连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理、React Native 组件布局、组件与 API 的介绍与代码实战,以及 React Native 与 iOS、Android 平台的混合开发底层原理讲解与代码实战演示,精选了大量实例代码,方便读者快速学习。 书籍还配套了视频教程「80 节实战课精通 React Native 开发」,此视频课程建议配合书籍学习,书籍中原理性的东西讲解的比较清晰,而视频教程对于组件、API 等部分的代码实战开发讲解比较直观。 书籍相关所有资料请访问:http://rn.parryqiu.com 本章将深入讲解 React Native 的底层原理,万丈高楼平地起,非常深入地理解 React Native 底层的实现,在你开发或遇到难题调试时非常有帮助。 此部分包含 React Native 的框架构成、工作原理、UI 层的渲染与重绘以及组件间通信、React Native 与各个平台的通信实现以及 React Native 中的生命周期。 如果需要直接开始 ...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS7安装Docker,走上虚拟化容器引擎之路
- CentOS7设置SWAP分区,小内存服务器的救世主
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS7,8上快速安装Gitea,搭建Git服务器
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- SpringBoot2全家桶,快速入门学习开发网站教程
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装