Android ConstraintLayout(约束布局)

1、概述

约束布局ConstraintLayout,是Android Studio 2.2中主要的新增功能之一,为了实现自适配

2、引入

app build.gradle中引入

implementation'com.android.support.constraint:constraint-layout:1.1.3'

在布局文件中就可以引入ConstraintLayout使用了

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.constraint.ConstraintLayout/>

 

3、相对布局属性

最基础的Constraint属性,可以设置在父控件内的位置,相对其他控件的相对位置,

layout_constraintLeft_toLeftOf              //同向左对齐

layout_constraintRight_toRightOf          //同向右对齐

layout_constraintTop_toTopOf                //同向顶对齐

layout_constraintBottom_toBottomOf     //同向底对齐

 

//start=left    end=right

layout_constraintRight_toLeftOf            //相邻

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio的约束布局是一种强大而灵活的布局方式,它允许您以声明性的方式定义视图之间的关系。以下是使用约束布局的一些基本步骤: 1. 在布局文件中使用`ConstraintLayout`作为根视图。在XML文件中,您可以这样声明一个约束布局: ``` <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 在这里添加其他视图 --> </androidx.constraintlayout.widget.ConstraintLayout> ``` 2. 定义视图之间的约束关系。您可以通过拖动和放置视图来在设计编辑器中设置约束,也可以在XML文件中手动编写约束。例如,要将一个按钮位于父布局顶部,并与左右边缘有10dp的间距,您可以这样定义约束: ``` <Button android:id="@+id/myButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="My Button" app:layout_constraintTop_toTopOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_marginStart="10dp" app:layout_marginEnd="10dp"/> ``` 3. 可选地,您还可以使用链(chains)和辅助对象(guidelines)来进一步定义视图之间的关系,以及在不同屏幕尺寸下的自适应布局约束布局的优势在于它可以适应各种屏幕尺寸和方向,并且可以减少嵌套布局的需要。您可以通过在Android Studio的设计编辑器中直观地操作视图和约束,或者手动编辑XML文件来创建约束布局。要了解更多关于约束布局的信息和用法,请参阅Android官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值