android约束布局

约束布局是谷歌在

使用:

为什么要使用约束布局:

  1. ConstraintLayout还有一个优点,它可以有效地解决布局嵌套过多的问题。
  2. 实现的布局效果类似于相对布局但比相对布局性能更高。
    盗个别人对比的图:
    这里写图片描述
    从图中我们可以看出性能上约束布局比相对布局性能要好上40%左右,因此有这么好的布局方式为什么不用呢。

使用方式:

1.在项目中添加依赖:

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

2.布局中使用:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

	<!--子控件-->

</android.support.constraint.ConstraintLayout>

实现子控件完全居中效果:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    android:background="@color/black">

    <!--垂直居中View-->
    <View
        android:id="@+id/view1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#00ff00"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <!--水平居中的View-->
    <View
        android:id="@+id/view2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#ff0000"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        />
    <!--完全居中的view-->
    <View
        android:id="@+id/view3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <!--右下角的View-->
    <View
        android:id="@+id/view4"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#0000ff"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        />

    <!--红色左侧部分居中及绿色上部分空白处居中-->
    <View
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#00ffff"
        app:layout_constraintRight_toLeftOf="@id/view2"
        app:layout_constraintLeft_toLeftOf="parent"

        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/view1"
        />
</android.support.constraint.ConstraintLayout>

效果图:
这里写图片描述
约束布局属性:

layout_constraintTop_toTopOf       // 将所需视图的顶部与另一个视图的顶部对齐。 

layout_constraintTop_toBottomOf    // 将所需视图的顶部与另一个视图的底部对齐。 

layout_constraintBottom_toTopOf    // 将所需视图的底部与另一个视图的顶部对齐。 

layout_constraintBottom_toBottomOf // 将所需视图的底部与另一个视图的底部对齐。 

layout_constraintLeft_toTopOf      // 将所需视图的左侧与另一个视图的顶部对齐。 

layout_constraintLeft_toBottomOf   // 将所需视图的左侧与另一个视图的底部对齐。 

layout_constraintLeft_toLeftOf     // 将所需视图的左边与另一个视图的左边对齐。 

layout_constraintLeft_toRightOf    // 将所需视图的左边与另一个视图的右边对齐。 

layout_constraintRight_toTopOf     // 将所需视图的右对齐到另一个视图的顶部。

layout_constraintRight_toBottomOf  // 将所需视图的右对齐到另一个的底部。

layout_constraintRight_toLeftOf    // 将所需视图的右边与另一个视图的左边对齐。

layout_constraintRight_toRightOf   // 将所需视图的右边与另一个视图的右边对齐。

大概解释:

  • constraintRight/Left/Top/Bottom
    布局的哪一边添加约束

  • toRight/Left/Top/BottomOf
    约束到哪个View的右/左/上/下边

注意:在添加外边距时如果没有在对应方向上添加约束则可能没有效果,比如一个View在parent左侧,且添加了marginLeft=20dp,则需要设置layout_constraintLeft_toLeftOf或者layout_constraintLeft_toRightOf才能使外边距生效。

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值