Android 机型适配之百分比适配 ConstraintLayout

由于Android的品类繁杂,机型适配向来是一个难题,常见的通过LinearLayout+RelativeLayout的方式进行适配具有较大的局限。而相比之下,百分比适配就强大很多了。

以往方式的弊端

  • RelativeLayout
    • 优点:相对布局,根据控件之间的相对关系互相依赖,使控件位置保持统一。
    • 缺点:无法保证控件尺寸的统一性。
  • LinearLayout
    • 优点:可通过权重布局,使控件尺寸保持统一。
    • 缺点:无法灵活设定控件的位置,并且只能控制单方向尺寸(横/竖)。
  • Dp
    • 优点:根据不同屏幕显示参数,动态计算值。
    • 缺点:无法做到精确控制,存在一定误差,且在极端情况下,无法适配。

百分比布局库

PercentLayout

Google官方提供的百分比布局库,包含PercentFrameLayoutPercentRelativeLayout。version 24.1.0引入,version 26.1.0被弃用(真的短命)。

导入方法:

    compile 'com.android.support:percent:26.1.0'

这也是最初本来准备尝试的库,因为不知道具体的使用方法于是去官网查文档,结果看到以下字段:

This class was deprecated in API level 26.1.0.
consider using ConstraintLayout and associated layouts instead. The following shows how to replicate the functionality of percentage layouts with a ConstraintLayout. The Guidelines are used to define each percentage break point, and then a Button view is stretched to fill the gap:

很无奈,刚准备学习一个新库,结果还没开始就已经被官方宣布寿终正寝。花了2秒感叹下生命周期的短暂,然后开始研究新库。想要参考下使用方法的可以点击官网进行阅读:PercentFrameLayout

ConstraintLayout

相对于PercentLayout来说ConstraintLayout享受的简直是亲儿子待遇,官方多次公开宣传过该控件,并配有视频等资源。

对我来说,只是在刚开始有了解过一些,最后判断该控件只是官方用来便于图形化搭建而诞生的,因此便没再过多研究。直到偶然机会发现这个布局竟然可以完成百分比布局的功能,所以才重新启用。

导入方法:

    compile 'com.android.support.constraint:constraint-layout:1.1.0-beta5'

Constraint

ConstraintLayout的核心就两个字:约束。除了普通的参数外核心参数有:

layout_constraintLeft_toLeftOflayout_constraintLeft_toRightOflayout_constraintRight_toLeftOflayout_constraintRight_toRightOflayout_constraintTop_toTopOflayout_constraintTop_toBottomOflayout_constraintBottom_toTopOflayout_constraintBottom_toBottomOflayout_constraintBaseline_toBaselineOflayout_constraintStart_toEndOflayout_constraintStart_toStartOflayout_constraintEnd_toStartOflayout_constraintEnd_toEndOf

这些参数理解起来很容易,由两个位置组成,第一个代表当前控件,第二个代表目标控件。例如:

 app:layout_constraintStart_toStartOf="parent"  控件左侧与父布局左侧建立约束 app:layout_constraintTop_toBottomOf="@id/button" 控件顶部与id为button的控件底部建立约束

建立了约束的两边就像是在之间安装了一个强力的弹簧,如果没有别的“弹簧”与之进行力的平衡,控件就会被拉过去。详情请想象简单的物理受力平衡。不明白的直接动手写,然后在布局Design选项能直接看到对应的受力模型,可以说做得很直观了。

Layout

建立好约束之后,默认情况每个约束的“力”都是相同的,例如:ConstraintLayout中有一个buttonbutton的四边分别约束了父布局的四边,就好像四条弹簧将button的四边和父布局的四边连起来了,因为弹簧的力是相同的,所以button最终的位置会在父布局的中央。如图:

而实际上这个“力”是可以不同的,通过以下参数可以设置:

app:layout_constraintHorizontal_bias="0.5"app:layout_constraintVertical_bias="0.5"

参数值以百分比表示,简单说明,以上述例子,如果app:layout_constraintVertical_bias="0.1",那么button整体会处在父布局竖直百分之10的位置上。至于怎么去理解这个整体处在,原谅我表达不是很好,不知道该怎么说,自己去体会吧。

通过调节这个参数便能以百分比的方式控制控件的所在位置了,实在是很方便。

Measure

主要分为三种:

WRAP_CONTENTMATCH_PARENTMATCH_CONSTRAINT

挨个说,前两个应该是无比熟悉了,使用效果和一般的没什么区别。重点是第三个,从名字上也能看出,由上面的约束所决定,当使用MATCH_CONSTRAINT时,控件仿佛一个橡皮泥,会被”弹簧“拉到无限远的地方,用刚才的button的例子说明,就是会被拉扯到父布局的大小。即会充满建立约束的边缘。

ps:这里有个小坑,看完官方文档知道我去设置MATCH_CONSTRAINT,但是编译器却始终没有提示,直接输入也会报错,最后又重新去查看文档,发现直接设置0dp就等于MATCH_CONSTRAINT,巨汗。

Percent dimension

To use percent, you need to set the following:

  • The dimension should be set to MATCH_CONSTRAINT (0dp)

百分比尺寸便是基于MATCH_CONSTRAINT实现的,在此基础上还需要设置另外一个参数:

app:layout_constraintWidth_percent=".5"app:layout_constraintHeight_percent=".5"

以上就是设置尺寸为父布局的百分之50,注意是父布局,该参数和具体建立约束的控件位置无关。

总结下:在设置MATCH_CONSTRAINT的前提下,默认尺寸为充满当前约束大小,在使用layout_constraintWidth_percentlayout_constraintHeight_percent能直接设置该控件为父布局的百分比大小与当前约束无关。

实战

以上就是通过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">            <Button        android:layout_width="0dp"        android:layout_height="0dp"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent"                app:layout_constraintHorizontal_bias="0.3"        app:layout_constraintVertical_bias="0.2"                app:layout_constraintHeight_percent=".1"        app:layout_constraintWidth_percent=".2"/>android.support.constraint.ConstraintLayout>

更多相关文章

  1. The Busy Coder's Guide to Android(安卓)Development 学习
  2. 记录用ConstraintLayout实现控件view最大高度的过程
  3. android之ListView自定义布局
  4. Android(安卓)分辨率与屏幕适配
  5. Android桌面组件App Widget用法入门教程
  6. Android开发04—Android常用高级控件(上)
  7. Android五大布局详解——LinearLayout(线性布局)
  8. Android布局优化总结
  9. Android(安卓)自定义View--ProgressBar篇(二)

随机推荐

  1. SQLite和MySQL数据库的差别与应用
  2. 用左连接查询大表真的很慢
  3. 如何在cakephp中找到最新的记录?
  4. 修改MySql服务名?
  5. MySQL学习笔记(十二)运算符和函数一
  6. 是java.sql.Date()和mysql命令sysdate
  7. Linux下修改MySQL用户(root)密码
  8. MySQL启动不起来和关闭不了的问题记录
  9. PHP发表心情-投票功能源码
  10. 如何通过mysql / php中的最佳匹配对搜索