本篇博客简单介绍下Android开发中RelativeLayout的用法。
新建一个空白项目时,activity_main.xml中的默认layout采用的就是RelativeLayout,RelativiLayout中的元素的定位方式是根据与其他控件的关系以及与父控件的关系来决定的。先来看一个简单的例子
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.zdk.relativelayoutdemo.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="我在底部"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="我在底部中间" android:id="@+id/textViewBottom"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="我在中间" android:id="@+id/textViewCenter"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="我在中间右边" android:layout_alignParentRight="true" android:layout_marginLeft="10dp" android:id="@+id/textViewCenterRight"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我在中间右边再往下" android:layout_below="@id/textViewCenterRight" android:layout_alignRight="@id/textViewCenterRight"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/textViewBottom" android:layout_alignParentBottom="true" android:text="我在底部右面" android:layout_marginLeft="5dp"/> </RelativeLayout>
这里面的元素要么指定了与父级容器的关系,要么指定了与其他控件的关系。下面来详细看一下常用到的属性
#指定与父容器的关系的常用属性
- layout_alignParentTop 定位到父容器的顶部
- layout_alignParentBottom 定位到父容器的底部
- layout_alignParentLeft 定位到父容器的左边
- layout_alignParentRight 定位到父容器的右边
- layout_centerHorizontal 水平方向居中显示
- layout_centerVertical 垂直方向居中显示
- layout_centerInParent 水平和垂直都居中显示
#指定与其它控件的关系的常用属性
首先要说的一点是,当你声明一个控件的id时,使用这种方式android:id="@+id/textViewCenterRight"
,@符号后面有个加号,而指定与某个控件的相对位置时,语法是这样的android:layout_below="@id/textViewCenterRight"
,区别是@符号后面没有加号。
- layout_above 位于某个控件的上面
- layout_below 位于某个控件的下面
- layout_toLeftOf 位于某个控件的左边
- layout_toRightOf 位于某个控件的右面
- layout_alignLeft 与某个控件左侧对齐
- layout_alignRight 与某个控件右侧对齐
- layout_alignTop 与某个控件顶侧对齐
- layout_alignBottom 与某个控件底侧对齐
- layout_alignBaseline 与某个控件内容对其,忽略border和padding
#控件出现的先后顺序
在Android1.6版本以前,后出现的控件可以相对于先出现的控件进行定位,反过来的话就会报错。但是Android1.6之后,这个问题已经不存在了。所以,我们也不需要考虑控件出现的先后顺序了。
最后
以上就是妩媚小蘑菇最近收集整理的关于Android开发从入门到放弃(4)使用RelativiLayout的全部内容,更多相关Android开发从入门到放弃(4)使用RelativiLayout内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复