Android ConstraintLayout 布局

AndroidStudio 3.0 默认ConstraintLayout布局,可以减少布局层级并提高布局性能;能够灵活的定位和调整子View的大小,子View依靠约束关系来确定位置。

一、基本属性
属性作用
layout_constraintLeft_toLeftOf左边左对齐
layout_constraintLeft_toRightOf左边右对齐
layout_constraintRight_toLeftOf右边左对齐
layout_constraintRight_toRightOf右边右对齐
layout_constraintTop_toTopOf上边顶部对齐
layout_constraintTop_toBottomOf上边底部对齐
layout_constraintBottom_toTopOf下边顶部对齐
layout_constraintBottom_toBottomOf下边底部对齐
layout_constraintBaseline_toBaselineOf文本内容基准线对齐
layout_constraintStart_toEndOf起始边向尾部对齐
layout_constraintStart_toStartOf起始边向起始边对齐
layout_constraintEnd_toStartOf尾部向起始边对齐
layout_constraintEnd_toEndOf尾部向尾部对齐

这些属性为控件添加了某个方向的约束力,根据某个方向约束力的“有无”和“强弱”,空间会位于不同的位置

<?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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <TextView
            android:id="@+id/tv1"
            android:background="@color/colorPrimary"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="tv1"
            android:gravity="center"
            android:textSize="20pt"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    <TextView
            android:id="@+id/tv2"
            android:background="@color/colorPrimary"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="tv2"
            android:gravity="center"
            android:textSize="20pt"
            app:layout_constraintBottom_toBottomOf="parent"
			app:layout_constraintLeft_toRightOf="@+id/tv1"
            app:layout_constraintTop_toBottomOf="@+id/tv1"/>
    <TextView
            android:id="@+id/tv3"
            android:background="@color/colorPrimary"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="tv3"
         android:gravity="center"
            android:textSize="20pt"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/tv2"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tv2"/>

</android.support.constraint.ConstraintLayout>

在这里插入图片描述

tv1设置了上、左的约束
tv2设置了左、上、下的约束,有上下居中的效果
tv3设置了上、下、左、右的约束,有居中的效果

二、偏移量
属性作用
layout_constraintHorizontal_bias水平方向偏移量
layout_constraintVertical_bias垂直方向偏移量
<?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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <TextView
            android:id="@+id/tv1"
            android:background="@color/colorPrimary"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="tv1"
            android:gravity="center"
            android:textSize="20pt"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>


</android.support.constraint.ConstraintLayout>

tv1设置了居中

在这里插入图片描述

<?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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <TextView
            android:id="@+id/tv1"
            android:background="@color/colorPrimary"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="tv1"
            android:gravity="center"
            android:textSize="20pt"
            app:layout_constraintVertical_bias="1"
            app:layout_constraintHorizontal_bias="0.8"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>


</android.support.constraint.ConstraintLayout>

设置它的垂直偏移量为1,水平偏移量为0.8

在这里插入图片描述

三、Visibility 属性

如果View的属性Visibility设置为gone,那么原本依赖它来参照定位的属性就会失效,ConstraintLayout布局会有所不同

layout_goneMarginStart
layout_goneMarginEnd
layout_goneMarginLeft
layout_goneMarginTop
layout_goneMarginRight
layout_goneMarginBottom

<?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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <TextView
            android:id="@+id/tv1"
            android:background="@color/colorPrimary"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="tv1"
            android:gravity="center"
            android:textSize="20pt"
            app:layout_constraintHorizontal_bias="0.2"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    <TextView
            android:id="@+id/tv2"
            android:background="@color/colorPrimary"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="tv2"
            android:gravity="center"
            android:textSize="20pt"
            app:layout_goneMarginTop="100dp"
            app:layout_constraintLeft_toRightOf="@+id/tv1"
            app:layout_constraintTop_toBottomOf="@+id/tv1"/>


</android.support.constraint.ConstraintLayout>

在这里插入图片描述

当tv1的Visibility 属性设置为gone时,tv1缩小为一个不可见的点,位于原先位置的左上角,属性app:layout_goneMarginTop="100dp"就会生效,tv2移到新的位置

在这里插入图片描述

四、宽高比

其他布局中要实现View的宽高比会比较麻烦,ConstraintLayout中可以直接实现:

  • 将View的宽高设置为0dp
  • 设置 app:layout_constraintDimensionRatio属性
<?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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <ImageView
            android:id="@+id/iv1"
            android:background="@color/colorPrimary"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintDimensionRatio="16:9"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

在这里插入图片描述
还支持这样的写法
app:layout_constraintDimensionRatio="H,16:9"
app:layout_constraintDimensionRatio="W,9:16"

五、控件权重

ConstraintLayout 可以像LinearLayout 一样为之控件设置layout_weight属性,从而控制子控件之间的占比

<?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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <TextView
            android:id="@+id/tv1"
            android:background="@color/colorPrimary"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:gravity="center"
            android:text="text1"
            app:layout_constraintHorizontal_weight="2"
            app:layout_constraintRight_toLeftOf="@id/tv2"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    <TextView
            android:id="@+id/tv2"
            android:background="@color/colorAccent"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:gravity="center"
            android:text="text2"
            app:layout_constraintHorizontal_weight="1"
            app:layout_constraintLeft_toRightOf="@id/tv1"
            app:layout_constraintRight_toLeftOf="@id/tv3"
            app:layout_constraintTop_toTopOf="parent"/>
    <TextView
            android:id="@+id/tv3"
            android:background="@color/colorPrimaryDark"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:gravity="center"
            android:text="text3"
            app:layout_constraintHorizontal_weight="1"
            app:layout_constraintLeft_toRightOf="@id/tv2"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>


</android.support.constraint.ConstraintLayout>

在这里插入图片描述
横向相当于一个链(Chains),在链的最左边的元素成为链头,还可以给它设置一些属性(layout_constraintHorizontal_chainStyle),来展示更多的效果

  • 当wight不等于0
    app:layout_constraintHorizontal_chainStyle="spread"(默认)
    在这里插入图片描述
    app:layout_constraintHorizontal_chainStyle="packed"
    在这里插入图片描述
    app:layout_constraintHorizontal_chainStyle="spread_inside"在这里插入图片描述
六、锚向指示线

其实指示线(Guideline)是来帮助定位的,不会出现在实际界面中,它有几个特性:

  • 宽度和高度都是0
  • 可见性为View.GONE
  • 通过orientation设置方向
<?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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <android.support.constraint.Guideline
            android:id="@+id/gl1"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    <android.support.constraint.Guideline
            android:id="@+id/gl2"
            app:layout_constraintGuide_begin="100dp"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    <TextView
            android:id="@+id/tv1"
            android:background="@color/colorPrimary"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="tv1"
            android:gravity="center"
            android:textSize="20pt"
            app:layout_constraintLeft_toLeftOf="@id/gl1"
            app:layout_constraintTop_toTopOf="@id/gl2"/>

</android.support.constraint.ConstraintLayout>

设置横向指示线距离顶部 100dp
竖向指示线设置其距离百分比为 0.5

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值