ConstraintLayout小记

作者: 有趣的灵魂200多斤 | 来源:发表于2017-10-09 15:53 被阅读0次

ConstraintLayout是Android Studio 2.2中主要的新增的布局,今天就来试着玩一下这个布局。
首先我们加入包

compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
示例1.png

如这样的界面,一般我们会使用RelativeLayout来做,现在我们来看下如何使用ConstraintLayout来做

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="net.zxl.constraintlayouttest.MainActivity">


    <ImageView
        android:id="@+id/img1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@mipmap/spider"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <TextView
        android:text="@string/content"
        android:textColor="#000000"
        app:layout_constraintTop_toBottomOf="@+id/img1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/img2"
        android:id="@+id/tv1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp" />
    <ImageView
        android:id="@+id/img2"
        app:layout_constraintTop_toTopOf="@+id/tv1"
        app:layout_constraintLeft_toRightOf="@+id/tv1"
        app:layout_constraintBottom_toBottomOf="@+id/tv1"
        app:layout_constraintRight_toRightOf="parent"
        android:background="@mipmap/man"
        android:layout_width="140dp"
        android:layout_marginRight="12dp"
        android:layout_height="0dp"
        android:layout_marginEnd="12dp" />
</android.support.constraint.ConstraintLayout>

layout_constraintLeft_toLeftOf  本控件的左边在谁的左边
layout_constraintRight_toRightOf 本控件的右边在谁的右边
layout_constraintTop_toTopOf 本控件的头部在谁的头部
layout_constraintTop_toBottomOf 本控件的头部在谁的底部
以此类推,parent代表当前的布局
示例2.png

在设置Button相互依靠以后如果使用的是宽度wrap_content并不会填充剩下的布局。

<Button
        android:id="@+id/button1"
        app:layout_constraintTop_toBottomOf="@+id/tv1"
        android:text="Button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/button2"
        android:text="Button2"
        app:layout_constraintTop_toBottomOf="@+id/tv1"
        app:layout_constraintLeft_toRightOf="@+id/button1"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/button3"
        app:layout_constraintTop_toBottomOf="@+id/button1"
        android:text="Button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/button4"
        android:text="Button4"
        app:layout_constraintTop_toBottomOf="@+id/button1"
        app:layout_constraintLeft_toRightOf="@+id/button1"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_width="0dp"
        android:layout_height="wrap_content" />

这里介绍一下 app:layout_constraintDimensionRatio="16:3" 是宽高比,但是我不太理解
https://developer.android.com/reference/android/support/constraint/ConstraintLayout.html#DimensionConstraints
大家可以互相交流。、
layout_constraintHorizontal_bias
layout_constraintVertical_bias
即设置上下两侧间隙比例分别为90%与10%。这个很好理解,我们之前说了,再没有bias这个属性的时候,这两侧的拉力大小是一样的,但是你可以通过bias来控制哪一侧的力要大一些

android.support.constraint.Guideline该类比较简单,主要用于辅助布局,即类似为辅助线,横向的、纵向的。该布局是不会显示到界面上的。
app:layout_constraintGuide_percent

<android.support.constraint.Guideline
        android:id="@+id/guideline_h"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.8" />


    <android.support.constraint.Guideline
        android:id="@+id/guideline_w"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.8" />
    <TextView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:background="#612"
        app:layout_constraintLeft_toRightOf="@id/guideline_w"
        app:layout_constraintTop_toBottomOf="@id/guideline_h" />
示例3
两条虚线就是Guideline。
好了,目前先记忆这些,demo地址是
https://github.com/primerToforget/constraintLayoutTest
欢迎大家交流学习
本文参考
http://blog.csdn.net/lmj623565791/article/details/78011599?utm_source=tuicool&utm_medium=referral
http://blog.csdn.net/guolin_blog/article/details/53122387
上面链接中有使用拖拉去实现约束的具体步骤,但还是建议学习使用代码约束

相关文章

网友评论

    本文标题:ConstraintLayout小记

    本文链接:https://www.haomeiwen.com/subject/fqkpyxtx.html