Android开发从入门到放弃(4)使用RelativiLayout
2016-12-24 12:09
309 查看
本篇博客简单介绍下Android开发中RelativeLayout的用法。
新建一个空白项目时,activity_main.xml中的默认layout采用的就是RelativeLayout,RelativiLayout中的元素的定位方式是根据与其他控件的关系以及与父控件的关系来决定的。先来看一个简单的例子
这里面的元素要么指定了与父级容器的关系,要么指定了与其他控件的关系。下面来详细看一下常用到的属性
layout_alignParentBottom 定位到父容器的底部
layout_alignParentLeft 定位到父容器的左边
layout_alignParentRight 定位到父容器的右边
layout_centerHorizontal 水平方向居中显示
layout_centerVertical 垂直方向居中显示
layout_centerInParent 水平和垂直都居中显示
layout_above 位于某个控件的上面
layout_below 位于某个控件的下面
layout_toLeftOf 位于某个控件的左边
layout_toRightOf 位于某个控件的右面
layout_alignLeft 与某个控件左侧对齐
layout_alignRight 与某个控件右侧对齐
layout_alignTop 与某个控件顶侧对齐
layout_alignBottom 与某个控件底侧对齐
layout_alignBaseline 与某个控件内容对其,忽略border和padding
新建一个空白项目时,activity_main.xml中的默认layout采用的就是RelativeLayout,RelativiLayout中的元素的定位方式是根据与其他控件的关系以及与父控件的关系来决定的。先来看一个简单的例子
<?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开发从入门到放弃(7)使用GridLayout
- Android开发从入门到放弃(3)使用SharedPreferences保存数据
- Android开发从入门到放弃(9)使用ListView显示一个类的数组
- Android开发从入门到放弃(8)使用ListView显示一个简单的列表
- android开发中监控android软件网络请求的软件Charles使用入门
- [Android开发入门]调用系统服务之Manager的使用(一)
- Android游戏开发框架Libgdx的使用(一)--Libgdx基础入门
- android开发之gridlayout使用入门
- Android开发中Handler使用入门
- Android开发使用控件入门--环境搭建
- Android--入门---3---(Android 系统的使用&开发前的设置)
- phonegap 开发入门 PhoneGap官方网站上有详细的入门示例教程,这里,我针对使用PhoneGap进行Android移动应用的开发对其官网的Get Started进行一些介绍、补充。 Ste
- 使用SpringMvc开发Android WebService接口入门教程
- android开发之gridlayout使用入门
- Android 百度地图开发教程(一) 地图使用入门及地图表面覆盖物使用
- Android入门开发之 Handler使用
- Android入门之旅5—使用Python脚本开发Android应用
- android开发入门-2(使用静态变量传递数据)
- 线程、消息-Android入门开发系列学习之 Handler使用-by小雨