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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java反射机制

    java反射机制学习spring提到spring框架中的setter方法是使用反射机制实现的,反射机制到底是什么呢?找了一篇文章Java基础与提高干货系列——Java反射机制Java基础与提高干货系列前言今天

    2022年7月2日
    24
  • 算法基础课acwing下载_十进制数135转换为八进制数

    算法基础课acwing下载_十进制数135转换为八进制数杭州人称那些傻乎乎粘嗒嗒的人为 62(音:laoer)。杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众。不吉利的数字为所有含有 4 或 62 的号码。例如:62315,73418,88914 都属于不吉利号码。但是,61152 虽然含有 6 和 2,但不是 连号,所以不属于不吉利数字之列。你的任务是,对于每次给出的一个牌照号区间 [n,m],推断出交管局今后又要实际上给多少辆新的士车上牌

    2022年8月11日
    4
  • 计算机病毒的分类

    计算机病毒的分类病毒与木马病毒:指编制或在计算机程序中插入的破坏计算机功能或破坏数据,影响计算机使用并且能够自我复制的一组计算机指令或程序代码。木马:是一种后门程序,被黑客用作控制远程计算机的工具。木马与病毒不同的是,木马不会自我繁殖,并不会刻意地感染其他文件,它的作用就是为黑客打开远程计算机的门户,从而可以让黑客来远程控制计算机,使黑客获取有用的信息。病毒是自动破坏目标计算机,而木马需要人为的去操控…

    2022年5月3日
    47
  • Mysql 培训

    Mysql 培训

    2021年11月23日
    52
  • Oracle修改字段类型Sql –ORA-01439

    Oracle修改字段类型Sql –ORA-01439有一个表名为tb,字段段名为name,数据类型nchar(20)。1、假设字段数据为空,则不管改为什么字段类型,可以直接执行:altertabletbmodify(namenvarchar2(40));2、假设字段有数据,则改为varchar2(40)执行时会弹出:“ORA-01439:要更改数据类型,则要修改的列必须为空”,这时要用下面方法来解决这个问题:–修改原字段…

    2022年5月26日
    80
  • path是什么意思啊_on the path

    path是什么意思啊_on the path投影投影是JMESPath的关键特性之一。它允许您将表达式应用于元素集合。有五种投影:列表投影切片投影对象投影展平投影过滤投影处理投影需要注意的点投影分为两个步骤。左侧(LHS)创建一

    2022年7月30日
    7

发表回复

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

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