安卓布局之约束布局

约束布局
约束布局ConstraintLayout 是一个ViewGroup,可以在Api9以上的Android系统使用它,它的出现主要是为了解决布局嵌套过多的问题,提升性能,更好的适配,以灵活的方式定位和调整小部件。从 Android Studio 2.3 起,官方的模板默认使用 ConstraintLayout。
形式:拖拽、代码
(1)相对定位left = start right = end
ConstraintLayout具有RelativeLayout的能力,可以将一个控件置于相对于另一个控件的位置。

   app:layout_constraintTop_toTopOf// 将所需视图的顶部与另一个视图的顶部对齐。 
	app:layout_constraintTop_toBottomOf // 将所需视图的顶部与另一个视图的底部对齐。 
	app:layout_constraintBottom_toTopOf// 将所需视图的底部与另一个视图的顶部对齐。 
	app:layout_constraintBottom_toBottomOf // 将所需视图的底部与另一个视图的底部对齐。 
	app:layout_constraintLeft_toLeftOf// 将所需视图的左边与另一个视图的左边对齐。 
	app:layout_constraintLeft_toRightOf// 将所需视图的左边与另一个视图的右边对齐。 
	app:layout_constraintRight_toLeftOf // 将所需视图的右边与另一个视图的左边对齐。
	app:layout_constraintRight_toRightOf // 将所需视图的右边与另一个视图的右边对齐。	
	app:layout_constraintBaseline_toBaselineOf//两个TextView的高度不一致,但是又希望他们文本对齐,这个时候就可以使用		

Eg:边距必须在有参照的情况下起作用

(2)边距
ConstraintLayout的边距常用属性如下:

						android:layout_marginStart
						android:layout_marginEnd
						android:layout_marginLeft
						android:layout_marginTop
						android:layout_marginRight
						android:layout_marginBottom
看起来跟别的布局没有什么差别,但实际上在ConstraintLayout中,layout_margin及其子属性的作用都被复写,margin属性只对其相约束的View起作用。
goneMargin主要用于约束的控件可见性被设置为gone的时候使用的margin值,属性如下:
app:layout_goneMarginStart
				app:layout_goneMarginEnd
				app:layout_goneMarginLeft
				app:layout_goneMarginTop
				app:layout_goneMarginRight
				app:layout_goneMarginBottom
				必须当参照的控件可见性被设置为gone时才生效
android:visibility="gone"
invisible //不显示(但是控件还占空间)
visible //显示
gone //不显示(控件所占用的空间也消失)

(3)居中和偏移
在RelativeLayout中把控件放在布局中间的方法是:

		android:layout_centerInParent="true"

而在ConstraintLayout中的写法是:约束布局宽或者高0dp拉伸

		app:layout_constraintBottom_toBottomOf="parent"
		app:layout_constraintLeft_toLeftOf="parent"
		app:layout_constraintRight_toRightOf="parent"
		app:layout_constraintTop_toTopOf="parent"

在RelativeLayout中,把控件水平居中和垂直居中

android:layout_centerHorizontal="true" //对于父控件横向居中
android:layout_centerVertical="true"//对于父控件纵向居中

在ConstraintLayout中:

对于父控件水平居中
layout_constraintLeft_toLeftOf & layout_constraintRight_toRightOf="parent"
对于父控件垂直居中
layout_constraintTop_toTopOf & layout_constraintBottom_toBottomOf="parent"

偏移:

app:layout_constraintHorizontal_bias 水平偏移
app:layout_constraintVertical_bias 垂直偏移

假如现在要实现水平偏移,给TextView1的layout_constraintHorizontal_bias赋一个范围为 0-1 的值,假如赋值为0,则TextView1在布局的最左侧,假如赋值为1,则TextView1在布局的最右侧,假如赋值为0.5,则水平居中,假如假如赋值为0.3,则更倾向于左侧。垂直偏移同理。
(4)尺寸约束

		layout_constraintWidth_min  最小宽度
		layout_constraintHeight_min 最小高度
		layout_constraintWidth_max    最大宽度
		layout_constraintHeight_max   最大高度
		layout_constraintWidth_percent  宽度占parent的百分比
		layout_constraintHeight_percent 高度占parent的百分比

(5)辅助线
通过设置Guideline的属性orientation来表示是水平方向还是垂直方向的参考线,对应值为vertical和horizontal。可以通过三种方式来定位Guideline位置。

<androidx.constraintlayout.widget.Guideline
    		android:id="@+id/guideline3"
   			 android:layout_width="wrap_content"
   			 android:layout_height="wrap_content"
    		android:orientation="horizontal"
   			 app:layout_constraintGuide_begin="174dp" />
<androidx.constraintlayout.widget.Guideline
   			 android:id="@+id/guideline2"
   			 android:layout_width="wrap_content"
   			 android:layout_height="wrap_content"
   			 android:orientation="horizontal"
   			 app:layout_constraintGuide_percent="0.14637482" />
layout_constraintGuide_begin 从左边或顶部指定具体的距离
layout_constraintGuide_end   从右边或底部指定具体的距离
layout_constraintGuide_percent 距离父容器左边或者顶部的距离占父容器的宽或者高的百分比
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值