android 布局之ConstraintLayout的使用

时间:2019-04-14
本文章向大家介绍android 布局之ConstraintLayout的使用,主要包括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>

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