相关文章推荐
高兴的蚂蚁  ·  MySQL DELETE 语句 | 菜鸟教程·  4 月前    · 
想发财的葡萄酒  ·  新聞-「2023新北市兒童田徑聯賽市錦賽」 ...·  4 月前    · 
慈祥的萝卜  ·  适用于 Dynamics 365 的 ...·  4 月前    · 
有胆有识的投影仪  ·  [新东方]英语学习-英语学习方法-英语学习资 ...·  4 月前    · 
豪气的人字拖  ·  配置 nginx ...·  1 年前    · 
Code  ›  Android ConstraintLayout百分比布局使用详解开发者社区
android javascript android布局 百分比
https://cloud.tencent.com/developer/article/2087667
知识渊博的斑马
1 年前
全栈程序员站长

Android ConstraintLayout百分比布局使用详解

前往小程序,Get 更优 阅读体验!
立即前往
腾讯云
开发者社区
文档 建议反馈 控制台
首页
学习
活动
专区
工具
TVP
最新优惠活动
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
全栈程序员站长
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
社区首页 > 专栏 > Android ConstraintLayout百分比布局使用详解

Android ConstraintLayout百分比布局使用详解

作者头像
全栈程序员站长
发布 于 2022-08-30 18:55:37
5.6K 0
发布 于 2022-08-30 18:55:37
举报
文章被收录于专栏: 全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

Android ConstraintLayout是谷歌推出替代PrecentLayout的组件。支持相对布局、线性布局、帧布局,笔者看来更像是 FrameLayout 、 LinearLayout 、 RelativeLayout 三者的结合体,并且比这三者更强大的是实现了百分比布局,大家都知道安卓碎片严重,使用百分比适配,那么将彻底解决适配问题。

使用小技巧:

Q:在约束布局中,wrap_content与0dp的区别:

A:

wrap_content:以内容的长度为准,一些比例属性会失效。

0dp:以控件的长度为准。

场景示例:

TextView显示文字内容,左边标题,右边是时间。

如果是wrap_content,左边标题过长,会覆盖到时间上方。

示例图片
示例图片

看懂了吧,所以在使用需要注意啦。

比如:以上场景,或者DimensionRatio,或者Percent等属性时。

Q:为什么约束布局刷新UI会卡顿

A:因为锚点没有设置完整,导致整个布局重新计算。

场景示例:

如果多个View左右关联,而两边不关联,就会造成整个布局重新计算绘制,造成UI卡顿。

Android ConstraintLayout百分比布局使用详解
Android ConstraintLayout百分比布局使用详解

将左右锚点加上之后,即可避免这种情况发生

—————–

百分比布局请滑到底部食用

本文将教会你如何使用此控件。

一、当作 RelativeLayout 使用

布局的逻辑是相同的,都是相对于某个View的上下左右方向。

代码语言: javascript
复制
layout_constraintLeft_toLeftOf:当前View左边在某个View的左边,可以是parent与某个View的ID
Android ConstraintLayout百分比布局使用详解
Android ConstraintLayout百分比布局使用详解
代码语言: javascript
复制
layout_constraintLeft_toRightOf:当前View左边在某个View的右边,可以是parent与某个View的ID
Android ConstraintLayout百分比布局使用详解
Android ConstraintLayout百分比布局使用详解

那如果这两种属性都加上,那么当前View就应该是父View左右居中的,看效果

Android ConstraintLayout百分比布局使用详解
Android ConstraintLayout百分比布局使用详解
代码语言: javascript
复制
layout_constraintRight_toRightOf:当前Viewr的右边在某个View的右边,可以是parent与某个View的ID
Android ConstraintLayout百分比布局使用详解
Android ConstraintLayout百分比布局使用详解
代码语言: javascript
复制
layout_constraintRight_toLeftOf:当前Viewr的右边在某个View的左边,可以是parent与某个View的ID
Android ConstraintLayout百分比布局使用详解
Android ConstraintLayout百分比布局使用详解
代码语言: javascript
复制
layout_constraintBottom_toBottomOf:当前Viewr的下边在某个View的下边,可以是parent与某个View的ID
Android ConstraintLayout百分比布局使用详解
Android ConstraintLayout百分比布局使用详解
代码语言: javascript
复制
layout_constraintBottom_toTopOf:当前Viewr的下边在某个View的上边,可以是parent与某个View的ID
Android ConstraintLayout百分比布局使用详解
Android ConstraintLayout百分比布局使用详解
代码语言: javascript
复制
layout_constraintTop_toTopOf:当前Viewr的上边在某个View的上边,可以是parent与某个View的ID
Android ConstraintLayout百分比布局使用详解
Android ConstraintLayout百分比布局使用详解
代码语言: javascript
复制
layout_constraintTop_toBottomOf:当前Viewr的上边在某个View的下边,可以是parent与某个View的ID
Android ConstraintLayout百分比布局使用详解
Android ConstraintLayout百分比布局使用详解

许多时候我们需要让子View与父View长度相同,只需要将layout_width或者layout_height设为0dp,让子View没有长度。这样便可以随着父View进入拉伸了

下面我们来实现一个常用的底部导航栏,5个导航栏

Android ConstraintLayout百分比布局使用详解
Android ConstraintLayout百分比布局使用详解
代码语言: javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tab0"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:background="@color/colorPrimary"
        android:gravity="center"
        android:text="tab1"
        android:textColor="@color/colorWhite"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/tab1" />
    <TextView
        android:id="@+id/tab1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="center"
        android:text="tab2"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/tab0"
        app:layout_constraintRight_toLeftOf="@+id/tab2"
        app:layout_constraintTop_toTopOf="@+id/tab0" />
    <TextView
        android:id="@+id/tab2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:text="tab3"
        android:textColor="@color/colorWhite"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/tab1"
        app:layout_constraintRight_toLeftOf="@id/tab3"
        app:layout_constraintTop_toTopOf="@+id/tab0" />
    <TextView
        android:id="@+id/tab3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="center"
        android:text="tab4"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/tab2"
        app:layout_constraintRight_toLeftOf="@+id/tab4"
        app:layout_constraintTop_toTopOf="@+id/tab0" />
    <TextView
        android:id="@+id/tab4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/colorHoloOrangeLight"
        android:gravity="center"
        android:text="tab5"
        android:textColor="@color/colorWhite"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/tab3"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/tab0" />
</android.support.constraint.ConstraintLayout>

要实现这种效果每个View需要相关联起来,一个链一个,最后手牵手拉伸成等比的View。

讲真的,一般实现这种效果,这种写法很繁复,使用LinearLayout可以说非常省心。但是ConstraintLayout可以一层就解决非常复杂的布局,这样实现不需要嵌套性能更好,对APP做优化往往就在这种细节的地方,如果对View的绘制感兴趣的朋友,可以找一下相关资料就明白了。

二、当作 LinearLayout 使用

与上文的(一、当作 RelativeLayout 使用)类似,只需要添加额外属性:

代码语言: javascript
复制
layout_constraintHorizontal_weight:横向的权重
代码语言: javascript
复制
layout_constraintVertical_weight:纵向的权重

如果上文中的tab3要大于其他四个tab,只需要在tab3的View添加app:layout_constraintHorizontal_weight=”2″,其他View设置为1,即可

Android ConstraintLayout百分比布局使用详解
Android ConstraintLayout百分比布局使用详解
代码语言: javascript
复制
 <TextView
        android:id="@+id/tab2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:text="tab3"
        android:textColor="@color/colorWhite"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_weight="2"
        app:layout_constraintLeft_toRightOf="@+id/tab1"
        app:layout_constraintRight_toLeftOf="@id/tab3"
        app:layout_constraintTop_toTopOf="@id/tab0" />
    <TextView
        android:id="@+id/tab3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="center"
        android:text="tab4"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toRightOf="@+id/tab2"
        app:layout_constraintRight_toLeftOf="@+id/tab4"
        app:layout_constraintTop_toTopOf="@+id/tab0" />

三、当作FrameLayout使用

不建议如此使用,没有这样的需求吧,与frameLayout使用相同

四、百分比布局(重点超大号字体)

百分比布局,意义非常重要,解决碎片化问题就是没有百分比的出现,现在我们来看一下,如何使用的:

代码语言: javascript
复制
layout_constraintVertical_bias:垂直乖离率(bias有道翻译为乖离率),也就是垂直偏移率。
代码语言: javascript
复制
layout_constraintHorizontal_bias:水平乖离率(bias有道翻译为乖离率),也就是水平偏移率。
代码语言: javascript
复制
layout_constraintHeight_percent:高度百分比,占父类高度的百分比
代码语言: javascript
复制
layout_constraintWidth_percent:宽度百分比,占父类宽度的百分比

假设一下场景,我们需要展示一个Banner,占屏幕的30%。

Android ConstraintLayout百分比布局使用详解
Android ConstraintLayout百分比布局使用详解
代码语言: javascript
复制
<TextView
        android:id="@+id/view0"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/colorPrimary"
        android:gravity="center"
        android:text="这是一个Banner"
        android:textColor="@color/colorWhite"
        android:textSize="30sp"
 
推荐文章
高兴的蚂蚁  ·  MySQL DELETE 语句 | 菜鸟教程
4 月前
想发财的葡萄酒  ·  新聞-「2023新北市兒童田徑聯賽市錦賽」 400位小選手跑跳出人生新高度-新北市政府體育局
4 月前
慈祥的萝卜  ·  适用于 Dynamics 365 的 Copilot | Microsoft Learn
4 月前
有胆有识的投影仪  ·  [新东方]英语学习-英语学习方法-英语学习资料-英语考试-新东方网_第354页
4 月前
豪气的人字拖  ·  配置 nginx 访问资源目录,nginx配置 root 与 alias 的区别 - 一文搞懂 - 博客园
1 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号