android ConstraintLayout android 布局之ConstraintLayout的使用

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

android ConstraintLayout android 布局之ConstraintLayout的使用

虾皮皮   2021-03-28 我要评论
想了解android 布局之ConstraintLayout的使用的相关内容吗,虾皮皮在本文为您仔细讲解android ConstraintLayout 的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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>

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们