ConstraintLayout(约束布局) part 1
来源:互联网 发布:尚学堂高淇java怎么样 编辑:程序博客网 时间:2024/06/05 11:39
ConstraintLayout
和它的布局编辑器将会在不久之后推出. 这篇文章所讲述的关于约束布局的内容是基于 constraint-layout:1.0.0-alpha4
. 它的一些行为最终在正式推出时可能发生些许改变。ConstraintLayout
容器用以在运行时解释这些约束.ConstraintLayout
容器的结构和工作原理. 未来,新的Android Studio 布局编辑器将会使它更加强大和灵活和易于管理该类布局. 通过更加深入的了解 ConstraintLayout
,我们将会更好的了解我们的开发工具。ConstraintLayout
被分配到了一个未绑定的支持库(support library)中 , 因此在该文中你将会看到许多的xml属性以 app:
开头而不是 android:
约束类型(Types of Constraints)
同级视图上的相关锚点
父容器的相关锚点
一条基线(稍后介绍)
Top, Bottom, Left, and Right (or Start and End)
CenterX and CenterY
基线
@id/button_cancel
的末尾和@id/button_next
的开始之间 ,如下所示:<Button
android:id="@+id/button_cancel"
… />
<Button
android:id="@+id/button_next"
app:layout_constraintStart_toEndOf="@+id/button_cancel"
… />
ConstraintLayout
XML示例:<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="@+id/constraintLayout"
android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"
android:layout_marginBottom="16dp" />
<Button
android:id="@+id/button_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="@+id/button_cancel"
android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"
android:layout_marginBottom="16dp"/>
</android.support.constraint.ConstraintLayout>
ConstraintLayout
.android:id
来引用targets。如果在约束加进来之前你的视图还没有定义id,Android Studio 会根据你在约束中声明的id为你创建一个在上面的案例中,你或许注意到了 ConstraintLayout
支持页边距(margins). 默认的, 两个视图之间的约束将会被解释为一个接一个的挨在一起.如果你想让你的视图之间留有空隙,那么margins是必须设置的。
它似乎很像 RelativeLayout
真是如此吗?
偏向约束(Biasing Constraints)
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
app:layout_constraintTop_toTopOf="@+id/constraintLayout"
app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"
app:layout_constraintStart_toStartOf="@+id/constraintLayout"
app:layout_constraintEnd_toEndOf="@+id/constraintLayout" />
</android.support.constraint.ConstraintLayout>
ConstraintLayout
采取了一个叫做刻度的概念,称之为偏向(bias). Bias 支持用权重来描述约束中非平均分布的空隙,例如:<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
app:layout_constraintTop_toTopOf="@+id/constraintLayout"
app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"
app:layout_constraintStart_toStartOf="@+id/constraintLayout"
app:layout_constraintEnd_toEndOf="@+id/constraintLayout"
app:layout_constraintHorizontal_bias="0.25"
app:layout_constraintVertical_bias="0.25" />
</android.support.constraint.ConstraintLayout>
LinearLayout
或 GridLayout
.中的权重 ,但是比它们更好。因为view并没有填满可用的空白区域。0.5
. 这就是视图默认居中的原因了。ConstraintLayout
扁平化视图层次的能力。它可以表现得像任何现有的布局管理器布局一样,或者说它们的所有表现!基线定锚(Anchoring to Guidelines)
有时候,为了对齐view,我们需要在一个地方有专门的锚点, ConstraintLayout
提供了基线(guidelines). 一个 Guideline
实际上是View
的子类 并且和其它子类一样添加. Guidelines 有一些特别的地方:
它们的测量宽度永远为0
它们的可见性为不可见(
View.GONE)
尽管它作为view存在于容器中,但他永远都不可见. 它的存在仅用于提供基线属性,为其它view定义水平或垂直的锚点. 请看一个示例:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline"
android:orientation="vertical"
app:layout_constraintGuide_begin="72dp" />
<Button
android:id="@+id/button_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="@+id/constraintLayout"
app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"
app:layout_constraintVertical_bias="0.25" />
<Button
android:id="@+id/button_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="@+id/constraintLayout"
app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"
app:layout_constraintVertical_bias="0.75" />
</android.support.constraint.ConstraintLayout>
一个垂直的基线被设置在距开始72dp 的位置. 每一个Button
现在可以使用该基线上的任意点作为它的 constraintStart
目标来排列对齐它们. Guidelines 可以有如下三个属性中的一个:
layout_constraintGuide_begin
:从开始边算起的绝对距离layout_constraintGuide_end
:从结束边算起的结束距离layout_constraintGuide_Percent
: 从开始边算起的百分百布局
处理约束的角度看, 一个基线上的所有锚点(水平或垂直).都是有用的目标,同样的,要有一个ID 。
In the current alpha, 这里有个bug,如果view引用该基线在前,而基线view之后才定义在后,将导致约束无法正常工作。为了正常工作,经常先加基线(在可视化编辑器中)或者手动的加入到xml的前面。
view的测量(View Measurements)
ConstraintLayout
支持一些你可能用到过的测量规则。 下面是关于子view的宽/高的三种测量规则:Exact: 一个精确的大小 (通常是给定的dp)
设置layout_width
或layout_height
为 一个非零大小
Wrap Content: 包裹 view’s内容
设置layout_width
或layout_height
为wrap_content
Any Size: 根据相关的约束条件填充可用的空白
设置layout_width
或layout_height
为0dp
那么, match_parent
到哪里去了?! ConstraintLayout
并不支持它, 并且接下来我会解释原因. 最近的版本说 any size 有效的替代了 match parent 的使用场景,并且更好的支持了约束规则。
任何在可视化界面中试图给view添加match_parent
的操作将会被覆盖, 并且如果你在 XML中添加它 ,将会产生十分笨拙的结果,请不要使用它 。
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="@+id/constraintLayout"
app:layout_constraintTop_toTopOf="@+id/constraintLayout"/>
<Button
android:id="@+id/button_next"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="@+id/button_cancel"
app:layout_constraintEnd_toEndOf="@+id/constraintLayout"
app:layout_constraintTop_toTopOf="@+id/constraintLayout"
app:layout_constraintBottom_toBottomOf="@+id/constraintLayout" />
</android.support.constraint.ConstraintLayout>
layout_width
为0dp
, 第二个按钮填充了从右边界到第一个按钮end边界之间的空隙. 这和框架中的权重十分的相似, 但是更加的灵活和实用。其它
UI布局第一个公共任务就是通过特定比例(aspect ratio)来测量view的大小 . 在图像(image)上用得十分频繁,无论它是矩形 (1:1
), 4:3
, 16:9
,或者其它自定义的形状。 使用ConstraintLayout
, 你不必再创建创建自定义的View
或ViewGroup
子类,由于 layout_constraintDimensionRatio
的存在。
该特征要求被关联的视图的尺寸"已知" (固定尺寸或 wrap_content
) 并且另外的是“any size” (0dp
). “any size” 尺寸将会由规则中定义的比例来分配。
<?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:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="@drawable/water"
app:layout_constraintDimensionRatio="16:9"
app:layout_constraintLeft_toLeftOf="@+id/constraintLayout"
app:layout_constraintTop_toTopOf="@+id/constraintLayout"
app:layout_constraintRight_toRightOf="@+id/constraintLayout"
app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"
app:layout_constraintVertical_bias="0.0" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="@drawable/grass"
app:layout_constraintDimensionRatio="4:3"
app:layout_constraintLeft_toLeftOf="@+id/constraintLayout"
app:layout_constraintRight_toRightOf="@+id/constraintLayout"
app:layout_constraintBottom_toBottomOf="@+id/constraintLayout" />
…
</android.support.constraint.ConstraintLayout>
SquareImageView
和AspectImageView
, 我们可能再也不需要你了- ConstraintLayout(约束布局) part 1
- Android ConstraintLayout 约束布局
- Android ConstraintLayout约束布局
- ConstraintLayout(约束布局)
- ConstraintLayout约束布局
- ConstraintLayout(约束布局)
- Android ConstraintLayout 约束布局
- Android约束布局ConstraintLayout
- 关于约束布局Constraintlayout
- ConstraintLayout约束布局
- Android ConstraintLayout 约束布局
- Android studio约束布局 ConstraintLayout
- Android ConstraintLayout 约束布局详解
- android ConstraintLayout约束布局详解
- ConstraintLayout约束布局学习笔记
- 约束布局ConstraintLayout加快布局速度
- 约束布局ConstraintLayout加快布局速度
- ConstraintLayout约束布局的概念与使用
- 牛客网做题学到的知识(四)
- POJ 2492 (树状数组统计数据)
- POJ2631Roads in the North
- c/c++第八天
- What Are You Talking About
- ConstraintLayout(约束布局) part 1
- iOS 第三方框架 - CocoaPods的Podfile语法参考
- ubuntu 16.04 上安装OpenStack Mitaka (all-in-one):环境准备
- JSP的内置对象之out,request对象练习
- linux 命令学习
- Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface androi
- C3P0的详细配置说明
- iOS_友盟统计接入
- 2016年8月3号