ConstraintLayout 完全解析

1、前言

ConstraintLayout刚出来的时候就关注了,只是学习了一下,一直没有记录,今天突然想用一下,但是好多要注意的点都想不起来了,就只能重新看一遍, 在此记录一下

2、layout_constraint[本源]_[目标]="[目标ID]"

app:layout_constraintBottom_toBottomOf="parent" // 底部与父控件底部对其
app:layout_constraintLeft_toLeftOf="parent"     //   左侧与父布局左侧对其
app:layout_constraintRight_toRightOf="parent"   // 右侧与父布局对其
app:layout_constraintTop_toTopOf="parent"       // 上边与父布局对其
app:layout_constraintHorizontal_bias="0.25" // 左侧空间与右侧空间加起来分四份,左侧占1分,右侧占3份
app:layout_constraintVertical_bias="0.25" // 上边空间与下边空间加起来分四份,上面一份,下面3份
注意:
(1)layout_constraintHorizontal_bias这个属性必须设置了居中才有效
(2)同时设置左右或者上下对齐表示左右或者上下居中
// 左右居中
app:layout_constraintLeft_toLeftOf="parent"    // 左侧与父布局左侧对其
app:layout_constraintRight_toRightOf="parent"  // 右侧与父布局对其
// 上下居中
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"

 3、如果一个button放在button1的右边,它的右边又与父控件的右边对其,那么如果这个控件的宽度设置为match_parent或者0dp,那么这个控件相当于设置了layout_weight=1,把剩余空间占满,如果设置了wrap_content,则在button1与父控件的剩余空间中居中

4、ConstraintLayout布局还可以使用constraintDimensionRatio设置视图的纵横比, 则需要把宽(layout_width)或者高(layout_height)设置为0dp,适合图片的适配

<android.support.constraint.ConstraintLayout
    android:id="@+id/constraintLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="200dp"
        android:background="@color/colorAccent"
        android:src="@drawable/total_large"
        app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"
        app:layout_constraintLeft_toLeftOf="@+id/constraintLayout"
        app:layout_constraintRight_toRightOf="@+id/constraintLayout"
        app:layout_constraintTop_toTopOf="@+id/constraintLayout"
        app:layout_constraintVertical_bias="0.0"/>

    <ImageView
        android:layout_width="0dp"
        android:layout_height="200dp"
        android:background="@color/colorAccent"
        android:contentDescription="@null"
        android:src="@drawable/total_large"
        app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"
        app:layout_constraintDimensionRatio="4:3"
        app:layout_constraintLeft_toLeftOf="@+id/constraintLayout"
        app:layout_constraintRight_toRightOf="@+id/constraintLayout"/>

</android.support.constraint.ConstraintLayout>

第一张图片宽设置为0,高被定死了,表示宽可以调整,宽高比设置为0,表示宽填满屏幕,高为200,

第二张图片宽设置为0,高也被定死了,比例设置为4:3,表示高200dp,宽为200dp*4/3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值