ConstraintLayout属性介绍及使用
文章目录
相信大家对ConstraintLayout并不陌生,Android Studio升级2.2之后,默认生成的XML布局已经从原来的RelativeLayout替换为ConstraintLayout,并且goole在去年的I/O大会上重点推出介绍了这个控件,对于ConstraintLayout到目前为止已经推出了很多的使用方法和属性还是比较给力的,并且到目前为止Google还在继续丰富其API和功能,所以这对于开发者而言是一个大的福音!下面我们就来看看它到底高明在哪里!!!
说明:因为文章中会用到大部分的ConstraintLayout的名词,我们在文章中会使用CL缩写来代替,使用RL代替RelativeLayout。
要引入ConstraintLayout的使用,我们需要在gradle文件中添加
implementation 'com.android.support.constraint:constraint-layout:1.1.0-beta3'
Relative positioning(相对位置)
循序渐进,我们从最基本的来,这是我使用ConstraintLayout写的一个布局效果:
这样的布局是我们平时很常见的效果,下面我们就看看使用ConstraintLayout时如何实现的:
<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.example.ljj.constraintlayoutdemo.demolist.RelativePositioningActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:title="相对位置"
app:titleTextColor="@android:color/white" />
<TextView
android:id="@+id/text_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/fab_margin"
android:layout_marginTop="@dimen/fab_margin"
android:text="奚梦瑶维密秀摔倒道歉"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar" />
<ImageView
android:id="@+id/image"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginTop="10dp"
android:scaleType="fitXY"
android:src="@drawable/image"
app:layout_constraintLeft_toLeftOf="@id/text_title"
app:layout_constraintTop_toBottomOf="@+id/text_title" />
<TextView
android:id="@+id/text_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="@dimen/fab_margin"
android:text="11月20日晚,一众国内外维密天使闪亮登场。但在表演环节中,奚梦瑶不慎出现意外跌倒在台上,意外发生后,奚梦瑶立刻整理了一下头发和身上的装饰,站起身来继续走T台,并且露出专业和自信的笑容,全场观众也为她拍手鼓励。"
app:layout_constraintLeft_toRightOf="@+id/image"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/image" />
<TextView
android:id="@+id/text_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="时间:2小时前"
app:layout