android技术Android开发Android知识

Constraint Layout 不完全使用手册

2017-01-12  本文已影响494人  潺森

[] Constraint Layout

how to use ConstraintLayout

Official Guide

build.gradle 文件中添加依赖

dependencies {
    compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
}

在使用过程中已经从 beta1 变成了 beta4 了。很快应该有正式版,注意更新版本。

两种方式使用 ConstraintLayout

把已有的 layout 转成 ConstraintLayout

注意是在Design 页面 中的Component Tree 中找到对应 layout, 右键,最后一项里 Convert xxxLayout to ConstraintLayout , 不是在 text页面。

自动转换的这种,编辑器会自动添加一些约束, 会尽力保持和原布局一致,但是在实际使用中发现自动转换的基本都不能用, 不如重新写来的方便。

Text页面中直接写 ConstraintLayout

重新写,或者直接修改原有 layout 都可以。

ConstraintLayout 约束constraint

看过官方文档中的视频介绍可以看到,视频中介绍了鼠标点击的方式和自动的方式来设置约束。 在我的实践中感觉鼠标点选问题只是看起来很美好,有如下问题

因此,我认为图形界面仅适用于看效果,和初期理解,务必坚持使用文本方式text的编辑方式

位置节点(handle or anchor)包括:

约束有下列要求:

约束constraint包括:

自动布局 不建议使用自动布局, 原因和自动转换时是一样的问题。

在约束下调整大小

3种类型:

调整 margin

和之前一样

新功能 调整占比 bias

相比原来的仅有的3种,增加了任意值的占比。比如占20% bias=0.2
原有的对齐方式就是占比中的3种特殊情况:

ConstraintLayout 的几个特性

常用功能

单个 View 布局:

水平对齐功能水平对齐功能
<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/common_blue_grey">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_blue"
        android:text="to parent left"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

</android.support.constraint.ConstraintLayout>

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/common_blue_grey">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_blue"
        android:text="to parent right"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

</android.support.constraint.ConstraintLayout>

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/common_blue_grey">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_blue"
        android:text="center horizontal"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

</android.support.constraint.ConstraintLayout>

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/common_blue_grey">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_blue"
        android:text="20% left of parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHorizontal_bias="0.2"
        />

</android.support.constraint.ConstraintLayout>

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/common_blue_grey">

    <android.support.constraint.Guideline
        android:id="@+id/vertical_20_percent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.2"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_blue"
        android:text="To left of a 20% guideline"
        app:layout_constraintLeft_toLeftOf="@id/vertical_20_percent"
        app:layout_constraintTop_toTopOf="parent"
        />

</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/common_blue_grey">

    <android.support.constraint.Guideline
        android:id="@+id/vertical_20_dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="20dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_blue"
        android:text="To left of a 20dp guideline"
        app:layout_constraintLeft_toLeftOf="@id/vertical_20_dp"
        app:layout_constraintTop_toTopOf="parent"
        />

</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/common_blue_grey">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_blue"
        android:layout_marginLeft="20dp"
        android:text="marginLeft 20dp to the parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

</android.support.constraint.ConstraintLayout>

两个 view 的布局

本节的内容包括:

top_topOftop_topOf
<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@color/common_blue_grey">

    <TextView
        android:id="@+id/top_target_text"
        android:layout_width="45dp"
        android:layout_height="wrap_content"
        android:background="@color/common_blue"
        android:text="Target"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <TextView
        android:id="@+id/top_topOf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_deep_orange"
        android:text="top_topOf"
        app:layout_constraintLeft_toRightOf="@id/top_target_text"
        app:layout_constraintTop_toTopOf="@id/top_target_text"
        />

    <TextView
        android:id="@+id/top_bottomOf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_deep_orange"
        android:text="top_bottomOf"
        app:layout_constraintLeft_toRightOf="@id/top_target_text"
        app:layout_constraintTop_toBottomOf="@id/top_target_text"
        />
</android.support.constraint.ConstraintLayout>
bottombottom
<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@color/common_blue_grey">

    <TextView
        android:id="@+id/bottom_target_text"
        android:layout_width="45dp"
        android:layout_height="wrap_content"
        android:background="@color/common_blue"
        android:text="Target"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <TextView
        android:id="@+id/bottom_topOf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_deep_orange"
        android:text="bottom_topOf"
        app:layout_constraintLeft_toRightOf="@id/bottom_target_text"
        app:layout_constraintBottom_toTopOf="@id/bottom_target_text"
        />

    <TextView
        android:id="@+id/bottom_bottomOf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_deep_orange"
        android:text="bottom_bottomOf"
        app:layout_constraintLeft_toRightOf="@id/bottom_target_text"
        app:layout_constraintBottom_toBottomOf="@id/bottom_target_text"
        />
</android.support.constraint.ConstraintLayout>
leftleft
<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@color/common_blue_grey">

    <TextView
        android:id="@+id/left_target_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_blue"
        android:text="Target Text is long"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <TextView
        android:id="@+id/left_leftOf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_deep_orange"
        android:textSize="11sp"
        android:text="left_leftOf"
        app:layout_constraintLeft_toLeftOf="@id/left_target_text"
        app:layout_constraintTop_toBottomOf="@id/left_target_text"
        />

    <TextView
        android:id="@+id/left_rightOf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_deep_orange"
        android:textSize="11sp"
        android:text="left_rightOf"
        app:layout_constraintLeft_toRightOf="@id/left_target_text"
        app:layout_constraintTop_toBottomOf="@id/left_target_text"
        />
</android.support.constraint.ConstraintLayout>
top_topOftop_topOf
<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@color/common_blue_grey">

    <TextView
        android:id="@+id/right_target_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_blue"
        android:text="Target Text is long"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <TextView
        android:id="@+id/right_leftOf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_deep_orange"
        android:textSize="11sp"
        android:text="right_leftOf"
        app:layout_constraintRight_toLeftOf="@id/right_target_text"
        app:layout_constraintTop_toBottomOf="@id/right_target_text"
        />

    <TextView
        android:id="@+id/right_rightOf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_deep_orange"
        android:textSize="11sp"
        android:text="right_rightOf"
        app:layout_constraintRight_toRightOf="@id/right_target_text"
        app:layout_constraintTop_toBottomOf="@id/right_target_text"
        />
</android.support.constraint.ConstraintLayout>
top_topOftop_topOf
<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@color/common_blue_grey">

    <TextView
        android:id="@+id/baseline_target_text"
        android:layout_width="45dp"
        android:layout_height="wrap_content"
        android:background="@color/common_blue"
        android:text="Target"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_deep_orange"
        android:textSize="11sp"
        android:text="baselineOf"
        app:layout_constraintLeft_toRightOf="@id/baseline_target_text"
        app:layout_constraintBaseline_toBaselineOf="@id/baseline_target_text"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_deep_orange"
        android:textSize="25sp"
        android:text="baselineOf"
        app:layout_constraintRight_toLeftOf="@id/baseline_target_text"
        app:layout_constraintBaseline_toBaselineOf="@id/baseline_target_text"
        />
</android.support.constraint.ConstraintLayout>

多个 view 的布局

本节的内容包括

gonegone
<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@color/common_blue_grey">

    <android.support.constraint.Guideline
        android:id="@+id/guideline_50"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp"/>

    <TextView
        android:id="@+id/gone_target_text_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_blue"
        android:text="Target"
        android:textSize="13sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="@id/guideline_50"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/right_bottom_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_deep_orange"
        android:text="bottom 1"
        android:textSize="11sp"
        android:visibility="gone"
        app:layout_constraintLeft_toLeftOf="@+id/gone_target_text_right"
        app:layout_constraintTop_toBottomOf="@+id/gone_target_text_right"
        />

    <TextView
        android:id="@+id/right_bottom_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_deep_orange"
        android:text="bottom 2"
        android:textSize="11sp"
        app:layout_constraintLeft_toLeftOf="@+id/gone_target_text_right"
        app:layout_constraintTop_toBottomOf="@+id/right_bottom_1"
        />

    <TextView
        android:id="@+id/gone_target_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_blue"
        android:text="Target"
        android:textSize="13sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/guideline_50"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/left_bottom_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_deep_orange"
        android:text="bottom 1"
        android:textSize="11sp"
        app:layout_constraintLeft_toLeftOf="@+id/gone_target_text"
        app:layout_constraintTop_toBottomOf="@+id/gone_target_text"
        />

    <TextView
        android:id="@+id/left_bottom_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_deep_orange"
        android:text="bottom 2"
        android:textSize="11sp"
        app:layout_constraintLeft_toLeftOf="@+id/gone_target_text"
        app:layout_constraintTop_toBottomOf="@+id/left_bottom_1"
        />
</android.support.constraint.ConstraintLayout>

marginGone 设置的依赖的 viewgone 时的 margin, 可以分别设置marginLeft,marginRight,marginTopmarginBottom.本例中设置的marginTop

gonegone

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@color/common_blue_grey">

    <android.support.constraint.Guideline
        android:id="@+id/guideline_50_margin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp"/>

    <TextView
        android:id="@+id/gone_margin_target_text_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_blue"
        android:text="Target"
        android:textSize="13sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="@id/guideline_50_margin"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/right_bottom_1_margin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_deep_orange"
        android:text="bottom 1"
        android:textSize="11sp"
        android:visibility="gone"
        app:layout_constraintLeft_toLeftOf="@+id/gone_margin_target_text_right"
        app:layout_constraintTop_toBottomOf="@+id/gone_margin_target_text_right"
        />

    <TextView
        android:id="@+id/right_bottom_2_margin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_deep_orange"
        android:text="bottom 2"
        android:textSize="11sp"
        app:layout_constraintLeft_toLeftOf="@+id/right_bottom_1_margin"
        app:layout_constraintTop_toBottomOf="@+id/right_bottom_1_margin"
        app:layout_goneMarginTop="11dp"
        />

    <TextView
        android:id="@+id/gone_margin_target_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_blue"
        android:text="Target"
        android:textSize="13sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/guideline_50_margin"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/left_bottom_1_margin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_deep_orange"
        android:text="bottom 1"
        android:textSize="11sp"
        app:layout_constraintLeft_toLeftOf="@+id/gone_margin_target_text"
        app:layout_constraintTop_toBottomOf="@+id/gone_margin_target_text"
        />

    <TextView
        android:id="@+id/left_bottom_2_margin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_deep_orange"
        android:text="bottom 2"
        android:textSize="11sp"
        app:layout_constraintLeft_toLeftOf="@+id/left_bottom_1_margin"
        app:layout_constraintTop_toBottomOf="@+id/left_bottom_1_margin"
        />
</android.support.constraint.ConstraintLayout>

style 里 的使用

ConstraintLayout 的属性多一些,写多了肯定想简化成 style 的形式。写成 style 时要注意ConstraintLayout 的属性属于自定义属性, 在 style 文件中不用写命名空间,直接写属性值即可。

stylestyle
<style name="ConstraintStyled">
    <item name="layout_constraintLeft_toLeftOf">parent</item>
    <item name="layout_constraintRight_toRightOf">parent</item>
    <item name="layout_constraintTop_toTopOf">parent</item>
    <item name="layout_constraintBottom_toBottomOf">parent</item>
</style>
<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@color/common_blue_grey">

    <TextView
        android:id="@+id/styled_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/common_blue"
        android:text="Target"
        android:textSize="13sp"
        style="@style/ConstraintStyled"
        />

</android.support.constraint.ConstraintLayout>

几个经验使用心得

github 地址

文中全部代码都在这里。欢迎交流。

https://github.com/buptfarmer/edu/blob/master/app/src/main/java/com/chensiwen/edugame/ConstraintLayoutActivity.java

上一篇 下一篇

猜你喜欢

热点阅读