Android笔记(八) Android中的布局——相对布局
RelativeLayout又称为相对布局,也是一种常用的布局形式。和LinearLayout的排列规则不同,RelativeLayout显得更加随意一下,它通常通过相对定位 的方式让控件出现在布局的任何位置。也正是因为如此,RelativeLayout中的属性非常多,不过这些属性都是有规律可循的。
我们通过代码来看:
relativelayout.xml
<?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"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button 1"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button 2"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button3"
android:layout_centerInParent="true"
android:text="Button 3"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button4"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Button 4"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button5"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Button 5"/>
</RelativeLayout>
运行结果为:
由此可见,这些属性和他们的名字一样,分别代表的含义为:
android:layout_alignParentLeft——和父布局的左对齐
android:layout_alignParentTop——和父布局上部对齐
android:layout_alignParentRight——和父布局的右对齐
android:layout_alignParentBottom——和父布局的下部对齐
android:layout_centerInParent——在父布局中居中显示
上面代码我们的控件是以父布局为标准的,在RelativeLayout中,同样也可以以控件为标准进行定位。
代码示例:
relativelayout.xml
<?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"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button3"
android:layout_centerInParent="true"
android:text="Button 3"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:layout_above="@id/button3"
android:layout_toLeftOf="@id/button3"
android:text="Button 1"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:layout_above="@id/button3"
android:layout_toRightOf="@id/button3"
android:text="Button 2"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button4"
android:layout_below="@id/button3"
android:layout_toLeftOf="@id/button3"
android:text="Button 4"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button5"
android:layout_below="@id/button3"
android:layout_toRightOf="@id/button3"
android:text="Button 5"/> </RelativeLayout>
运行结果为:
这些属性也有规律可循:
android:layout_above——表示该控件在标准控件上方
android:layout_below——表示该控件在标准控件下方
android:layout_toLeftOf——表示该控件在标准控件左面
android:layout_ toRightOf——表示该控件在标准控件右面
但需要注意的是,这些属性需要使用“@id/xxxx”来引入标准控件的id,并且当一个控件以另外一个控件为标准时,另一个控件一定要定义在这个控件前面,否则会出现找不到id的情况。
RelativeLayout中还有另外一组相对于控件进行定位的属性
android:layout_alignRight——表示该控件的右边缘和标准控件的右边缘对齐
android:layout_ alignLeft——表示该控件的左边缘和标准控件的左边缘对齐
android:layout_alignTop——表示该控件的上边缘和标准控件的上边缘对齐 android:layout_alignBottom——表示该控件的下边缘和标准控件的下边缘对齐
Android笔记(八) Android中的布局——相对布局的更多相关文章
- Android 笔记之 Android 系统架构
Android笔记之Android系统架构 h2{ color: #4abcde; } a{ color: blue; text-decoration: none; } a:hover{ color: ...
- Android笔记: Android版本号
由于有2套版本号 总是对应不准 记下来做过标记 Android 4.3 ----18 Android 4.2---17 Android 4.1---16 Android 4.0.3---15Andro ...
- Android笔记:java 中的数组
在与嵌入式设备通讯的过程中使用的socket通讯 获取的字节流,通常转换为字节数组,需要根据协议将字节数组拆分.对于有规律的重复拆分可以使用,由于java中不能像c中直接进行内存操作例如使用struc ...
- Android笔记:java 中的枚举
部分数据使用枚举比较方便,java中的enmu不如c#中使用方便 记录备忘 以c#中的代码为例 public enum PlayState { /// <summary> /// 关闭 / ...
- Android笔记之Fragment中创建ViewModel的正确方式
之前一直都是这么写的 pageViewModel = ViewModelProviders.of(this).get(PageViewModel.class); //参数this是当前fragment ...
- Android笔记:android的适配
public int Dp2Px(Context context, float dp) { final float scale = context.getResources().getDisplayM ...
- Android笔记(十一) Android中的布局——网格布局
网格布局是Android4.0新增的布局管理器,因此需要在Android4.0之后的版本才可以使用,之前的平台使用该布局的话,需要导入相应的支持库. GridLayout的作用类似于HTML中的tab ...
- Android笔记(十) Android中的布局——表格布局
TableLayout运行我们使用表格的方式来排列控件,它的本质依然是线性布局.表格布局采用行.列的形式来管理控件,TableLayout并不需要明确的声明包含多少行多少列,而是通过添加TableRo ...
- Android笔记(九) Android中的布局——框架布局
框架布局没有任何定位方式,所有的控件都会摆放在布局的左上角. 代码示例: framelayout.xml <?xml version="1.0" encoding=" ...
随机推荐
- LODOP中设置设置图片平铺水印,超文本透明
之前的博文:LODOP中平铺图片 文本项Repeat. 该博文中是平铺的图片,上面是文本.如果是图片add_print_image和add_print_text纯文本,这两个打印项设计的,可以直接通过 ...
- 原生JavaScript常用本地浏览器存储方法五(LocalStorage+userData的一个浏览器兼容类)
基于LocalStorage+globalStorage+userData实现的一个本地存储类 userData用来兼容ie6 ie7 由userData模仿Session的方法:浏览器关闭删除保存的 ...
- Input.GetMouseButtonDown 在fixedupdate中会出现丢失问题,在update中则完全没这个问题
Input.GetMouseButtonDown 在fixedupdate中会出现丢失问题,在update中则完全没这个问题
- [概率DP][消元法][CF24D]损坏的机器人
Description 有一只坏了的机器人站在一个\(n\times m\)的网格里,初始位置在\((x,y)\).现在每个单位时间内它会随机选左右下三个方向走,如果它随机的方向会走出网格就不会往这个 ...
- Beta冲刺(4/4)
队名:秃头小队 组长博客 作业博客 组长徐俊杰 过去两天完成的任务:学习了很多东西 Github签入记录 接下来的计划:继续学习 还剩下哪些任务:细节处理 燃尽图 遇到的困难:自己太菜了 收获和疑问: ...
- PHP curl模拟ip和来源进行访问
PHP curl模拟ip和来源进行访问<pre> public function moniurlqingqiu() { $ch = curl_init(); $curlurl = &quo ...
- 【坑】SpringMvc 处理JSON 乱码
文章目录 前言 方法 前言 在使用 springMvc 的时候,如果向前台返回 JSON 数据,JSON 中的中文会乱码: 即使你在配置了全局的信息编码拦截器,也无济于事: 原因大抵是,JSON 的内 ...
- (一)Spring Security Demo 登陆与退出
文章目录 配置springSecurityFilterChain过滤器 配置身份验证 加载配置 登陆项目 退出 下面的代码需要spring环境的支持: 看这个系列博客之前,需要这个博客,大概了解下 s ...
- 解决windows下tomcat端口被占用
在平时开发中,电脑太卡或者项目比较大,有时候没有完全停止eclipse下的tomcat再次启动tomcat会出现端口占用情况,主要报如下错误: 具体解决方案: 打开windows下的命令窗口(快捷键: ...
- Linux基础-13-源码安装软件包
1.准备 安装必要软件 yum install gcc-* glibc-* -y yum groupinstall '开发工具' -y 2.解包 tar xvf 包名 3.运行configure脚本, ...