您当前的位置:首页 > Android教程

Android开发学习教程(14)- Android布局之相对布局RelativeLayout

时间:2022-01-10 07:28:17 阅读数:26,742人阅读
版权声明:转载请注明出处,谢谢!
—— 珍贵的东西往往数目稀少,所以你要变得更强,才有力量去抢。

上一篇我们讲了线性布局LinearLayout的基本用法,这里来学习常用布局之相对布局RelativeLayout的基本用法。

相对布局是什么

相对布局按照控件间的相对位置排列,比如控件A相对于控件B在控件B的左边,并且和控件B顶部对齐等。

相对布局有什么用

通过相对位置来控制控件在屏幕的位置。

相对布局怎么用

继续基于上一篇的项目,我们新建一个RelativeLayoutActivity:

	<?xml version="1.0" encoding="utf-8"?>
	<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
		android:layout_width="match_parent"
		android:layout_height="match_parent">

		<TextView
			android:id="@+id/tv1"
			android:layout_width="wrap_content"
			android:layout_height="100dp"
			android:layout_centerInParent="true"
			android:background="#F0F0F0"
			android:gravity="center"
			android:text="我是第一个子控件" />

		<TextView
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:layout_alignTop="@+id/tv1"
			android:layout_toLeftOf="@+id/tv1"
			android:text="我是第二个子控件" />

		<TextView
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:layout_alignTop="@+id/tv1"
			android:layout_toRightOf="@+id/tv1"
			android:text="我是第三个子控件" />

		<TextView
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:layout_alignBottom="@+id/tv1"
			android:layout_toLeftOf="@+id/tv1"
			android:text="我是第四个子控件" />

		<TextView
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:layout_alignBottom="@+id/tv1"
			android:layout_toRightOf="@+id/tv1"
			android:text="我是第五个子控件" />

	</RelativeLayout>

第一个控件

	<TextView
		android:id="@+id/tv1"
		android:layout_width="wrap_content"
		android:layout_height="100dp"
		android:layout_centerInParent="true"
		android:background="#F0F0F0"
		android:gravity="center"
		android:text="我是第一个子控件" />
	android:layout_height="100dp":表示TextView控件高度为100dp
	android:layout_centerInParent="true":表示TextView控件位于父控件RelativeLayout的水平居中并且垂直居中
	android:gravity="center":表示TextView控件上面显示的内容水平居中并且垂直居中

第二个控件

	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_alignTop="@+id/tv1"
		android:layout_toLeftOf="@+id/tv1"
		android:text="我是第二个子控件" />
	android:layout_alignTop="@+id/tv1":表示与id为tv1控件顶部对齐
	android:layout_toLeftOf="@+id/tv1":表示在id为tv1控件的左边

第三个控件

	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_alignTop="@+id/tv1"
		android:layout_toRightOf="@+id/tv1"
		android:text="我是第三个子控件" />
	android:layout_alignTop ="@+id/tv1"表示与id为tv1控件顶部对齐
	android:layout_toRightOf ="@+id/tv1"表示在id为tv1控件的右边

第四个控件

	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_alignBottom="@+id/tv1"
		android:layout_toLeftOf="@+id/tv1"
		android:text="我是第四个子控件" />
	android:layout_alignBottom="@+id/tv1"表示与id为tv1控件底部对齐
	android:layout_toLeftOf="@+id/tv1"表示在id为tv1控件的左边

第五个控件

	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_alignBottom="@+id/tv1"
		android:layout_toRightOf="@+id/tv1"
		android:text="我是第五个子控件" />
	android:layout_alignBottom="@+id/tv1"表示与id为tv1控件底部对齐
	android:layout_toRightOf="@+id/tv1"表示在id为tv1控件的右边

RelativeLayout常用基本属性

第一类属性 属性值为true或者false

	android:layout_centerHrizontal:水平居中

	android:layout_centerVertical:垂直居中

	android:layout_centerInparent:相对于父控件完全居中

	android:layout_alignParentBottom:贴紧父控件的下边缘

	android:layout_alignParentLeft:贴紧父控件的左边缘

	android:layout_alignParentRight:贴紧父控件的右边缘

	android:layout_alignParentTop:贴紧父控件的上边缘

第二类属性 属性值必须为id的引用名“@id/id-name”

	android:layout_below:在某控件下方
	
	android:layout_above:在某控件上方
	
	android:layout_toLeftOf:在某控件的左边
	
	android:layout_toRightOf:在某控件的右边
	
	android:layout_alignTop:本控件的上边缘和某控件的上边缘对齐
	
	android:layout_alignLeft:本控件的左边缘和某控件的左边缘对齐
	
	android:layout_alignBottom:本控件的下边缘和某控件的下控件对齐
	
	android:layout_alignRight:本控件的右边缘和某控件的有边缘对齐

本篇源码下载地址:https://pan.baidu.com/s/1cmYguZgyqkXBiMd8kWjyHA 提取码: helu

------转载请注明出处,感谢您对原创作者的支持------

有偿提供技术支持、Bug修复、项目外包、毕业设计、大小作业

Android学习小站

Q Q:1095817610

微信:jx-helu

邮箱:1095817610@qq.com

添加请备注"Android学习小站"