温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

android中怎么使用ConstraintLayout 布局

发布时间:2021-06-27 14:13:53 来源:亿速云 阅读:225 作者:Leah 栏目:移动开发

这期内容当中小编将会给大家带来有关android中怎么使用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>

android中怎么使用ConstraintLayout 布局

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

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

比如第一个控件的:

app:layout_constraintTop_toTopOf="parent"

第二个控件的

app:layout_constraintTop_toBottomOf="@+id/tv1"

字面意思就是:

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

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

android中怎么使用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"
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 布局了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI