Android ConstraintLayout百分比布局使用详解

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

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

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

使用小技巧:

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

A:

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

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

场景示例:

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

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

示例图片

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

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

 

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

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

场景示例:

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

Android ConstraintLayout百分比布局使用详解

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

—————–

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

 

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

一、当作RelativeLayout使用

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

layout_constraintLeft_toLeftOf:当前View左边在某个View的左边,可以是parent与某个View的ID

Android ConstraintLayout百分比布局使用详解

layout_constraintLeft_toRightOf:当前View左边在某个View的右边,可以是parent与某个View的ID

Android ConstraintLayout百分比布局使用详解

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

Android ConstraintLayout百分比布局使用详解

layout_constraintRight_toRightOf:当前Viewr的右边在某个View的右边,可以是parent与某个View的ID

Android ConstraintLayout百分比布局使用详解

layout_constraintRight_toLeftOf:当前Viewr的右边在某个View的左边,可以是parent与某个View的ID

Android ConstraintLayout百分比布局使用详解

layout_constraintBottom_toBottomOf:当前Viewr的下边在某个View的下边,可以是parent与某个View的ID

Android ConstraintLayout百分比布局使用详解

layout_constraintBottom_toTopOf:当前Viewr的下边在某个View的上边,可以是parent与某个View的ID

Android ConstraintLayout百分比布局使用详解

layout_constraintTop_toTopOf:当前Viewr的上边在某个View的上边,可以是parent与某个View的ID

Android ConstraintLayout百分比布局使用详解

layout_constraintTop_toBottomOf:当前Viewr的上边在某个View的下边,可以是parent与某个View的ID

Android ConstraintLayout百分比布局使用详解

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

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

Android ConstraintLayout百分比布局使用详解

<?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使用)类似,只需要添加额外属性:

layout_constraintHorizontal_weight:横向的权重
layout_constraintVertical_weight:纵向的权重

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

Android ConstraintLayout百分比布局使用详解

 <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使用相同

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

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

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

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

Android ConstraintLayout百分比布局使用详解

<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"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHeight_percent="0.3"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0" />

只需要添加属性app:layout_constraintHeight_percent=”0.3″占父类的30%,如果仅仅设置这一个属性,你会发现Banner居中了,你还差一个属性,表示从垂直的偏移量: app:layout_constraintVertical_bias=”0″偏移量为0,如果就可以了。

使用百分比布局时,View必须要设置上下左右四个锚点,如果不设置就像射线一样,都不知道多大,如何百分比呢?

当锚点是parent(也就是屏幕),因为分辨率不一样,使用百分比的view占的位置、大小肯定是不相同的,720的50%等于360,而1080的50%是等于590,仅仅是看起来位置相同,实际并不相同,所以当百分比与固定大小结合实现布局时,应当注意锚点不要给错了,还可以给到某个固定大小的View身上,如果View宽度是跟随父View,也应当注意。

下面再看一个例子:

Android ConstraintLayout百分比布局使用详解

可以看出树始终是在屏幕中间的,而圈起来的地方是不一样的,这就是因为屏幕分辨率不一样造成的。相应的可以知道,如果红包的锚点是对应的屏幕,那就达不到我们想要的适配效果了。而树是一张固定大小的图片,对应它的百分比不管在哪都是一样的,所以上面讲的应当注意锚点,就是如此了。

实战百分比适配,可以看下另一篇文章

官方介绍传送门

还有一些属性笔者使用很少,未能深入理解,本文后期再做更新

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/145012.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 手机靓号正则表达式「建议收藏」

    手机靓号正则表达式「建议收藏」手机靓号正则表达式手机靓号正则ABC+=1234或12345…AAAA+=1111或11111…AAAA+//ABC+DDD+//AA+BBB+//AABBCC//ABABABAAAA+    (\d)\1{3,}   //如果想调3A+改{3,}改成{2,},想固定3A的话把{2,}的{,}号去掉  (1111+)ABC+DDD+  (?:0(?=1)|1(?=2)|2(?=3)|3(?=4

    2022年6月8日
    54
  • 高并发下线程安全的单例模式(最全最经典)

    高并发下线程安全的单例模式(最全最经典)在所有的设计模式中,单例模式是我们在项目开发中最为常见的设计模式之一,而单例模式有很多种实现方式,你是否都了解呢?高并发下如何保证单例模式的线程安全性呢?如何保证序列化后的单例对象在反序列化后任然是单例的呢?这些问题在看了本文之后都会一一的告诉你答案,赶快来阅读吧!

    2022年5月16日
    36
  • C语言每日小练(四)——勇者斗恶龙「建议收藏」

    C语言每日小练(四)——勇者斗恶龙

    2022年2月6日
    39
  • 数据结构中的elem,elemtype是什么

    数据结构中的elem,elemtype是什么elem是单词element(元素)的缩写,在程序定义中代表某一不确定的类型,也就是抽象的数据类型。为了使程序可读性强,并且便于修改,让elem代表多种的数据类型,也就是为int、char等等的数据类型,起了一个别名。ElemType是数据结构的书上为了说明问题而用的一个词。它是elementtype(“元素的类型”)的简化体。 因为数据结构是讨论抽象的数据结构和算法的,一种结构中元素的类型…

    2022年5月12日
    80
  • modelsim安装_Modelsim10.5安装教程

    modelsim安装_Modelsim10.5安装教程1.鼠标右击软件压缩包,选择“解压到modelsim-win64-10.5”。2.打开解压后的文件夹,鼠标右击“modelsim-win64-10.5”,选择“以管理员身份运行”。3.点击“下一步”。4.点击“浏览”选择软件的安装路径(建议安装在C盘以外的其他磁盘,且安装路径不要有中文),点击“下一步”。…

    2022年5月23日
    124
  • goland 激活码2021[免费获取]

    (goland 激活码2021)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlMLZPB5EL5Q-eyJsaWNlbnNlSWQi…

    2022年3月20日
    50

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号