一、前言:
Android里的ConstraintLayout是个非常强大的工具,它有效的解决了Android里Layout的层级嵌套的问题。使用一个ConstraintLayout可以实现之前多个Layout才能实现的效果。
1、constraintLayout的一些高级用法 布局一个16:9的图片
<!-- "W,9:16" 同样的效果 -->
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
android:src="@mipmap/icon"
app:layout_constraintDimensionRatio="H,16:9"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
二、Guideline介绍:
1、Guideline
Guideline
是只能用在ConstraintLayout布局里面的一个工具类,用于辅助布局,类似为辅助线,可以设置android:orientation
属性来确定是横向的还是纵向的。
- 当设置为
vertical
的时候,Guideline
的宽度为0,高度是parent
也就是ConstraintLayout的高度 - 同样设置为
horizontal
的时候,高度为0,宽度是parent
的宽度
重要的是Guideline是不会显示到界面上的,默认是GONE的。
Guideline
还有三个重要的属性,每个Guideline
只能指定其中一个:
-
layout_constraintGuide_begin
,指定左侧或顶部的固定距离,如100dp,在距离左侧或者顶部100dp的位置会出现一条辅助线 -
layout_constraintGuide_end
,指定右侧或底部的固定距离,如30dp,在距离右侧或底部30dp的位置会出现一条辅助线 -
layout_constraintGuide_percent
,指定在父控件中的宽度或高度的百分比,如0.8,表示距离顶部或者左侧的80%的距离。
2、实例1
- Button1:依赖的基线,基线距离左侧 50dp;
- Button2:依赖的基线,基线距离右侧 150dp;
- Button3:依赖的基线,基线占据百分比70%;
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guidelineBegin"
app:layout_constraintGuide_begin="50dp"
android:orientation="vertical"/>
<Button
android:text="Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
app:layout_constraintLeft_toLeftOf="@+id/guidelineBegin"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guidelineEnd"
app:layout_constraintGuide_end="150dp"
android:orientation="vertical"/>
<Button
android:text="Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonEnd"
app:layout_constraintRight_toLeftOf="@+id/guidelineEnd"
android:layout_marginTop="48dp"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guidelinePercent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.7" />
<Button
android:text="Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonPercent"
app:layout_constraintLeft_toLeftOf="@+id/guidelinePercent"
android:layout_marginTop="96dp"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
3、使用Guideline实现控件对称布局
假如,我们想要实现如下的效果,控件A和控件B,在屏幕上对称显示。
在这个例子里,我们可以先定义一个垂直的Guideline,然后用这行代码layout_constraintGuide_percent="0.5"让Guideline垂直居中:
<android.support.constraint.Guideline
android:id="@+id/guide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5"/>
然后以这根Guideline为基准,左右各constraint一个控件,注意这两行代码app:layout_constraintRight_toLeftOf="@id/guide"
和app:layout_constraintLeft_toRightOf="@id/guide"
:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"
app:layout_constraintRight_toLeftOf="@id/guide"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="200dp"
android:layout_marginRight="50dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"
app:layout_constraintLeft_toRightOf="@id/guide"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="200dp"
android:layout_marginLeft="50dp"/>
效果如下图,在App真正运行的时候,中间那根线是看不见的。