android 布局之ConstraintLayout的使用

其实ConstraintLayout是Android Studio 2.2中主要的新增功能之一,也是Google在2016年的I/O大会上重点宣传的一个功能。是为了android可视化编辑而做的努力。android studio 的可视化编辑个人不推荐使用,不过ConstraintLayout布局的使用还是有必要了解的。

1,要想使用ConstraintLayout需要在app的build.gradle里面引入:

compile 'com.android.support.constraint:constraint-layout:1.0.2'

2,首先看一个简单的xml和图片效果:

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.app.qichun.hellowrod.MainActivity">

<TextView
  android:id="@+id/tv1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Hello World!"
  app:layout_constraintTop_toTopOf="parent"
 />
<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="第二个控件"
  app:layout_constraintLeft_toLeftOf="parent"
  app:layout_constraintTop_toBottomOf="@+id/tv1"
  />
<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="第三个控件"
  app:layout_constraintBottom_toBottomOf="parent"
  />
<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="第四个控件"
  app:layout_constraintLeft_toRightOf="@+id/tv1"
  android:layout_marginLeft="10dp"
  />
 </android.support.constraint.ConstraintLayout>

各位看客官不难看出,xml中四个简单的Textview分布位置,以id为tv的第一个控件为基准,第二个控件在第一个控件的下方,且都在整个布局的左边;第三个控件在整个父布局的左下方;第四个控件在第一个控件的右边。

仔细观察,每个Textview都有类似的属性:

比如第一个控件的:

app:layout_constraintTop_toTopOf="parent"

第二个控件的

app:layout_constraintTop_toBottomOf="@+id/tv1"

字面意思就是:

该控件的某个边和某个控件的某个边对齐。

比如,第一个控件是该控件的上部和父布局的上部对齐,自然就使得第一个控件处于左上方;第二个控件的顶部和第一个控件即id=tv1的控件的底部对齐,自然第二个 控件就会位于第一个控件的正下方。其他举一反三即可。
现在我们规定一个布局再次试验一下,搞个最常见的布局。

代码如下:

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.app.qichun.hellowrod.MainActivity">

<TextView
  android:id="@+id/tv1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="任务"
 <!-- 该控件的顶部和父布局的顶部对齐 !-->
  app:layout_constraintTop_toTopOf="parent"
 />
<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="已完成"
  <!-- 该控件的右边和父布局的左边对齐 !-->
  app:layout_constraintRight_toLeftOf="parent"
  android:layout_marginRight="10dp"
  />
<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="刘刚"
 <!-- 该控件的顶部和tv1的底部对其齐!-->
  app:layout_constraintTop_toBottomOf="@+id/tv1"
  />
<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="完成时间"
  <!-- 该控件的右边和父布局的左边对齐 !-->
  app:layout_constraintRight_toLeftOf="parent"
 <!-- 该控件的顶部和tv1的底部对齐 !-->
  app:layout_constraintTop_toBottomOf="@+id/tv1"
  android:layout_marginRight="10dp"
  />
</android.support.constraint.ConstraintLayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

您可能感兴趣的文章:

  • 详解Android ConstraintLayout 约束布局的用法
  • Android新特性ConstraintLayout完全解析
  • Android新布局方式ConstraintLayout快速入门教程
  • Android利用ConstraintLayout实现漂亮的动画详解
(0)

相关推荐

  • Android新布局方式ConstraintLayout快速入门教程

    前言 在Android开发中,我们通常是手写布局,很少会用拖动来写布局,虽然ConstraintLayout在I/O上以拖动来展现了各种功能,我估计在以后开发中,程序员还是习惯手撸代码. 我自己试着拖着用了一下,用得不是很明白 ,而且用起来效果不是很好. 那么 直接上手撸了一下~~~ 其实很简单 Button1:app:layout_constraintBottom_toTopOf="@id/iv_head" 我们把这个属性拆开来看,constraintBottom指的本身的底部,即B

  • 详解Android ConstraintLayout 约束布局的用法

    前言 在2016年的Google I/O大会上 , Google 发布了Android Studio 2.2预览版,同时也发布了Android 新的布局方案 ConstraintLayout , 但是最近的一年也没有大规模的使用.2017年Google发布了 Android Studio 2.3 正式版,在 Android Studio 2.3 版本中新建的Module中默认的布局就是 ConstraintLayout .如下所示: <?xml version="1.0" enc

  • Android新特性ConstraintLayout完全解析

    本文同步发表于我的微信公众号,在微信搜索 郭霖 即可关注,每天都有文章更新. 今天给大家带来2017年的第一篇文章,这里先祝大家新年好. 本篇文章的主题是ConstraintLayout.其实ConstraintLayout是Android Studio 2.2中主要的新增功能之一,也是Google在去年的I/O大会上重点宣传的一个功能.我们都知道,在传统的Android开发当中,界面基本都是靠编写XML代码完成的,虽然Android Studio也支持可视化的方式来编写界面,但是操作起来并不方

  • Android利用ConstraintLayout实现漂亮的动画详解

    前言 最近ConstrainLayout是Android中比较火的一个东西.ConstrainLayout可以使View层级扁平化,提升性能,支持任意的边框,其目的就是修复之前layout的一些短板.其实ConstrainLayout还有一个大多数人没有注意到的特性:可以利用Constrainlayout快速构建漂亮的动画效果. 方法 我这里假设已经你已经掌握了Constrainlayout基本知识(比如:app:layout_constraintLeft_toLeftOf等).Constrai

  • android 布局之ConstraintLayout的使用

    其实ConstraintLayout是Android Studio 2.2中主要的新增功能之一,也是Google在2016年的I/O大会上重点宣传的一个功能.是为了android可视化编辑而做的努力.android studio 的可视化编辑个人不推荐使用,不过ConstraintLayout布局的使用还是有必要了解的. 1,要想使用ConstraintLayout需要在app的build.gradle里面引入: compile 'com.android.support.constraint:c

  • Android布局ConstraintLayout代码修改约束及辅助功能

    目录 实践过程 代码修改约束 辅助功能 Guideline Barrier Flow Placeholder Group和Layer 实践过程 代码修改约束 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/an

  • android布局优化的一些实用建议

    前言 Android的绘制优化其实可以分为两个部分,即布局(UI)优化和卡顿优化,而布局优化的核心问题就是要解决因布局渲染性能不佳而导致应用卡顿的问题,所以它可以认为是卡顿优化的一个子集. 本文主要包括以下内容 为什么要进行布局优化及android绘制,布局加载原理 获取布局文件加载耗时的方法 介绍一些布局优化的手段与方法 一些常规优化手段 为什么要进行布局优化? 为什么要进行布局优化?答案是显而易见的,如果布局嵌套过深,或者其他原因导致布局渲染性能不佳,可能会导致应用卡顿 那么布局到底是如何导

  • Android布局之GridLayout网格布局

    网格布局标签是GridLayout.这个布局是android4.0新增的布局.这个布局只有4.0之后的版本才能使用. 不过新增了一些东东 ①跟LinearLayout(线性布局)一样,他可以设置容器中组件的对齐方式 ②容器中的组件可以跨多行也可以跨多列(相比TableLayout直接放组件,占一行相比较) 因为是android 4.0新增的,API Level 14,在这个版本以前的sdk 都需要导入项目,等下会详细介绍 常用属性: 排列对齐: ①设置组件的排列方式:   android:ori

  • Android布局之FrameLayout帧布局

    前言 作为android六大布局中最为简单的布局之一,该布局直接在屏幕上开辟出了一块空白区域, 当我们往里面添加组件的时候,所有的组件都会放置于这块区域的左上角; 帧布局的大小由子控件中最大的子控件决定,如果都组件都一样大的话,同一时刻就只能能看到最上面的那个组件了! 当然我们也可以为组件添加layout_gravity属性,从而制定组件的对其方式 帧布局在游戏开发方面用的比较多,等下后面会给大家演示一下比较有意思的两个实例 (-)帧布局简介 帧布局容器为每个加入的其中的组件创建一个空白的区域称

  • Android布局之TableLayout表格布局

    Tablelayout类以行和列的形式对控件进行管理,每一行为一个TableRow对象,或一个View控件.当为TableRow对象时,可在TableRow下添加子控件,默认情况下,每个子控件占据一列. 当为View时,该View将独占一行. 三个常用的属性 android:collapseColumns:设置需要被隐藏的列的序号 android:shrinkColumns:设置允许被收缩的列的列序号 android:stretchColumns:设置运行被拉伸的列的列序号 学习导图 (1)Ta

  • Android布局之RelativeLayout相对布局

    RelativeLayout是相对布局控件:以控件之间相对位置或相对父容器位置进行排列. 相对布局常用属性: 子类控件相对子类控件:值是另外一个控件的id android:layout_above----------位于给定DI控件之上 android:layout_below ----------位于给定DI控件之下  android:layout_toLeftOf -------位于给定控件左边 android:layout_toRightOf ------位于给定控件右边  android

  • Android布局之LinearLayout线性布局

    LinearLayout是线性布局控件:要么横向排布,要么竖向排布 常用属性: android:gravity------------设置的是控件自身上面的内容位置 android:layout_gravity-----设置控件本身相对于父控件的显示位置 android:layout_weight----- 给控件分配剩余空间 先给大家展示一下导图: 知识点详解(演示效果方便组件没有设置id) (1)gravity和Layout_gravity android:gravity 属性是对该view

  • Android布局之LinearLayout自定义高亮背景的方法

    本文实例讲述了Android布局之LinearLayout自定义高亮背景的方法.分享给大家供大家参考,具体如下: 首先创建linearlayout_background.xml文件 res/drawable/linearlayout_background.xml <?xml version="1.0" encoding="utf-8"?> <selectorxmlns:android="http://schemas.android.com

随机推荐