ConstraintLayout 想说爱你不容易 (二)

简介: 日常补漏,学习一下 ConstraintLayout 基本用法!

      和尚在很久以前了解过 ConstraintLayout 的基本用法,但实际应用的却比较少;近来和尚在尝试一些历史项目的优化,对于 View-UI 绘制方面,准备通过 ConstraintLayout 来减少绘制层级;

Bias 偏向

      ConstraintLayout 可以通过 _bias 属性设置偏向于水平或竖直某一端;

  1. 使用 _bias 属性时需要关联水平或竖直方向的两端关联;若未设置关联关系,单纯的 _bias 偏向无效果;
  2. _bias 偏向与权重无关系,所谓偏向,是水平或竖直某一端距边缘剩余空间的占比;
  3. _bias 默认为 0.5 居中展示,整体范围是 [0.0, 1.0]
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Center B (水平+竖直)"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

Circular positioning 圆形定位

      ConstraintLayout 可以通过半径和角度来设置两个控件的关联约束;

  1. app:layout_constraintCircle 用来设置关联的控件,以此控件作为圆形中心;
  2. app:layout_constraintCircleAngle 用来设置两个控件的相对角度,其范围是 [0, 360],以顺时针方向,竖直向上的为 0,与数学中的坐标系一致;
  3. app:layout_constraintCircleRadius 作为圆形的半径设置两个控件的距离;

Button
    android:id="@+id/circle_btn"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:background="@drawable/btn_circle_red"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
    
<Button
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:background="@drawable/btn_circle_green"
    app:layout_constraintCircle="@+id/circle_btn"
    app:layout_constraintCircleAngle="90"
    app:layout_constraintCircleRadius="160dp" />

Group 组的显隐性

      ConstraintLayout 减少了控件的 View 层级,但对于多个控件的显隐形可以通过 Group 来处理;Group 通过设置 referenced_ids / setReferencedIds 将设置的控件数组统一管理,直接设置 Group 的显隐性即可;

<androidx.constraintlayout.widget.Group
    android:id="@+id/constract_group"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:constraint_referenced_ids="circle_item_btn1,circle_item_btn2,circle_item_btn3" />

mGroup.setVisibility(isVisible ? View.VISIBLE : View.GONE);

Chains 链式结构

      和尚在使用 ConstraintLayout 时,对于多个控件的整体居中尝试了链式结构;在学习过程中发现 Chains 有更多实用的效果;

      和尚需要设置几个控件共同居中,且这几个控件的间距位置不变;

  1. 编辑设置多个预展示的控件;

  1. 选中需要设置的多个控件,右键 Chains 设置 Create Vertical Chains;如图展示,虽然整体居中,但控件间的相对间隔也改变了;因为默认的 _chainStylespread 样式;

  1. 设置最顶部链头属性 app:layout_constraintVertical_chainStyle="packed"

      链式结构的整体效果主要是通过 Chains Head 链头来决定!

ChainStyle - spread

      spread 为平均分散排列,及各个控件间的距离是均分的(不包括控件自身设置的 margin / padding 属性);

ChainStyle - spread_inside

      spread_inside 也是平均分散排列,其中两端默认是紧临水平或竖直两端的(不包括控件自身设置的 margin / padding 属性);

ChainStyle - packed

      packed 是把链式结构中关联的控件组作为一个整体进行排列,可以设置 Bias 整体偏向等;

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/textView"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="今日推荐" android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/imageView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:layout_marginTop="30dp" android:src="@mipmap/icon_hzw"
        app:layout_constraintBottom_toTopOf="@+id/textView2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_marginTop="20dp" android:text="布鲁克"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="20sp" android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/textView5"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_marginTop="20dp" android:background="@drawable/btn_circle_gray"
        android:padding="10dp" android:text="点击详情"
        android:textColor="@android:color/darker_gray" android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />
</androidx.constraintlayout.widget.ConstraintLayout>

Weighted Chains 权重链

      类似于 LinearLayout 中的 widget 权重,ConstraintLayout 也可以通过 _weight 设置权重效果;其中使用权重时需优先设置好 Chains 链式结构;

<View
    android:id="@+id/view"
    android:layout_width="0dp"
    android:layout_height="120dp"
    android:background="@color/colorPrimary"
    app:layout_constraintEnd_toStartOf="@+id/view2"
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintStart_toStartOf="parent" />

<View
    android:id="@+id/view2"
    android:layout_width="0dp"
    android:layout_height="120dp"
    android:background="@color/colorAccent"
    app:layout_constraintEnd_toStartOf="@+id/view3"
    app:layout_constraintHorizontal_weight="2"
    app:layout_constraintStart_toEndOf="@+id/view" />

<View
    android:id="@+id/view3"
    android:layout_width="0dp"
    android:layout_height="120dp"
    android:background="@color/colorPrimary"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintStart_toEndOf="@+id/view2" />

Gone Margins 隐藏外边距

      在应用中,margins 为外边距,在使用 ConstraintLayout 关联布局时,添了 _goneMargin 属性;即控件 B 是以控件 A 作为关联基础,当控件 A 动态隐藏时,可以通过 _goneMargin 设置控件 B 的新的外边距;其中 _goneMargin 属性需要设置在非隐藏控件 B 中;

<TextView
    android:id="@+id/view7"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_marginLeft="15dp"
    android:background="@color/colorPrimary"
    android:text="A"
    android:visibility="gone"
    app:layout_constraintEnd_toStartOf="@+id/view8"
    app:layout_constraintStart_toStartOf="@+id/imageView"
    app:layout_constraintTop_toBottomOf="@+id/item_tv3" />

<TextView
    android:id="@+id/view8"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_marginLeft="15dp"
    android:background="@color/colorAccent"
    android:text="B"
    app:layout_constraintEnd_toStartOf="@+id/view9"
    app:layout_constraintStart_toEndOf="@+id/view7"
    app:layout_constraintTop_toBottomOf="@+id/item_tv3"
    app:layout_goneMarginLeft="30dp"
    app:layout_goneMarginStart="30dp" />


      和尚这次整理的还很浅显,未曾设计源码,主要是之前应用较少的场景,对于层级的优化还是很重要的;如有错误,请多多指导!

来源: 阿策小和尚

目录
相关文章
|
11月前
|
XML Java Android开发
Android ExpandableListView 使用中遇到的问题集锦
Android ExpandableListView 使用中遇到的问题集锦
|
缓存 IDE 前端开发
致终将逝去的 Android
致终将逝去的 Android
89 0
致终将逝去的 Android
|
监控 开发工具 Android开发
国内硬件开发者如何看待Android Wear
今日凌晨,Google对外发布专门为可穿戴设备而优化的Android Wear平台,并宣布跟多个大厂合作开发硬件产品,其中摩托罗拉的“Moto 360”宣传视频引入到文章里面。
175 0
国内硬件开发者如何看待Android Wear
|
Web App开发 搜索推荐 Linux
控制力的较量 Android上的博弈
  前言   开源这个开放源代码的中文缩写,这个承载了软件行业光荣与梦想的单词,造就了Emacs、Linux、Netscape、Firefox、MySQL、Apache、JBoss这样可以与商业软件媲美的自由软件。
1194 0
|
Android开发 UED iOS开发
Android UI之困 横跨四个屏幕的战争
  用户界面(UI)的话题,总是会带来强烈的争论。这有点像电视领域——每个人都是专家,因为每个人都是用户。早在2002年,电信运营商就有一套自己的UI定制界面——例如Vodafone Live 和 Orange SPV。
953 0
|
机器学习/深度学习 Java 程序员
Android 凉了?聊聊Android的出路,架构必备技术
从现在很多 Android 培训广告来看,Android 没凉,似乎还回暖了,不过不可否认,现在做 Android 确实比以前难过,其实每个行业发展到一定程度都会饱和,这都是属于正常情况,如果你第一份工作是 Android,如今工作三四年,想必也会遇到技术瓶颈,无关 Android 是否没人要,也...
|
Android开发
Android小黑科技——来自火星的你
今天偶然看到网友的微信地区是一个魔法学院,微信的地区怎么可能是魔法学院呢?肯定是这位网友自己搞了一些黑科技,然后改的。他能改,我们也能改,二话不说就开干。
926 0
|
Android开发
Android开发 - 掌握ConstraintLayout(七)辅助线(Guideline)
了解过UI设计的同学都知道,在设计的时候,我们经常在界面上拖进一些辅助线来帮我们对齐UI元素,或者方便我们统一的页边距。 在ConstraintLayout的编辑器中,同样也支持这样的功能,我们可以创建一些横向的或者纵向的Guideline,在布局界面的时候可以充分利用这些辅助线,对齐我们的View,避免重复写一些marginXXX。
1508 0
|
Android开发 vr&ar 算法
android招聘啦,美图秀秀欢迎你加入!
android招聘啦,美图秀秀欢迎你加入!
1101 0