ConstraintLayout属性介绍及使用

本文详细介绍了Android的ConstraintLayout,包括MATCH_CONSTRAINT、居中与偏移、圆形定位、Margin、Ratio、Chains和GuideLine等属性的使用,帮助开发者理解和优化Android布局设计。
摘要由CSDN通过智能技术生成

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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值