Android 约束布局(ConstraintLayout)1

本文详细介绍了Android的ConstraintLayout,包括圆形定位、强制约束、尺寸设定、间距和链、优化器以及屏障特性。通过实例展示了如何使用ConstraintLayout进行更灵活的布局设计,以提高开发效率。
摘要由CSDN通过智能技术生成

准备

implementation ‘com.android.support.constraint:constraint-layout:1.1.0’

Circular Positioning

圆形定位(Circular Positioning)可以让一个控件以另一个控件的中心为中心点,来设置其相对与该中心点的距离和角度。 可以设置的属性有:

  • layout_constraintCircle:引用另一个控件的 id。
  • layout_constraintCircleRadius:到另一个控件中心的距离。
  • layout_constraintCircleAngle:控件的角度(顺时针,0 - 360 度)。

下面以给头像设置 badge 为例,演示下其用法:

<?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.github.airsaid.constraintlayoutdemo.MainActivity”>

</android.support.constraint.ConstraintLayout>

运行结果:

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

Enforcing constraints

在 1.1 版本之前,如果将控件的尺寸设置为了 WRAP_CONTENT,那么对控件设置约束(如:minWidth 等)是不起作用的。那么强制约束(Enforcing constraints)的作用就是,在控件被设置 WRAP_CONTENT 的情况下,使约束依然生效。

需要使用到的属性有:

  • app:constrainedWidth=“true|false”
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值