Android相对布局(RelativeLayout)
Android相对布局(RelativeLayout)
备注:这里的视图和元素是等同的概念。
RelativeLayout是一个允许子视图相对于其他兄弟视图或是父视图显示的视图组(通过ID指定)。每个视图的位置能够指定它相对于兄弟(比如在其他视图的左边或是下边)或是父视图(这里是指相对布局容器,比如底部对齐、中间偏左)的位置。
图1
RelativeLayou是一个用于设计用户界面的强大工具,因为它能消除嵌套视图组和保持我们布局为扁平结构,这可以提高运行时性能。如果我们采用了多个嵌套的LinearLayout组,我们应该采用一个单独的RelativeLayout来替代。
1. 配置视图(positioning views)
我们能够通过右边界对其两个元素,或是使一个元素位于两一个下面,居中在显示屏上、左居中、等等。默认情况下,所有的子视图在布局的左上角显示,所以我们必须使用RelavieLayout.LayoutParams中多种多样的布局属性来定义每个视图的位置。
每个布局属性值既可以是一个确定相对于父容器RelativeLayout布局位置的boolean类型,也可以是某个子视图的ID,即此视图相对于其他视图的位置。
在我们的XML布局文件中,可以以任何顺序来声明布局中相对于其他视图的关系。比如,我们可以声明view1在view2的下面,尽管view2在布局层次的最后申明。
2. 相对布局常用的属性
分为几类:
(1) 布局容器中通用的,不管是相对布局、线性布局等布局容器。
Android:gravity设置该布局容器内个子组件的对齐方式,比如button组件中内容在button中的对齐方式,比如:
<Button
android:id="@+id/button1"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="确认"
/>
如果没有社会自gravity属性值,button的内容默认是在button组件居中显示的,而且可以看到button是位于左上角的,如下图:
图2
我们增加android:gravity="left",让“确认”在button的左边显示,如下图:
图3
Android:ignoreGravity设置哪个组件不受gravity属性的影响。
(2) 属性值为具体像素值的属性,如30dip,40px
android:layout_marginBottom 离某元素底边缘的距离
android:layout_marginLeft 离某元素左边缘的距离
android:layout_marginRight 离某元素右边缘的距离
android:layout_marginTop 离某元素上边缘的距离
下面两种属性是由RelavieLayout.LayoutParams定义。
(3) 属性值为true或是false的属性
android:layout_alignParentBottom控制该组件是否和布局管理器底端对齐。
android:layout_alignParentEnd控制该组件是否和布局管理器末端对齐。
android:layout_alignParentLeft 控制该组件是否和布局管理器左边对齐
android:layout_alignParentRight控制该组件是否和布局管理器右边对齐
android:layout_alignParentStart控制该组件是否和布局管理器开始对齐
android:layout_alignParentTop控制该组件是否和布局管理器顶部对齐
android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物
先来看下面内容
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按键2"
/>
的布局界面:
图4
我们增加android:layout_alignParentTop="true",还是和图4一样,是因为这里只是让按键2和其父容器顶部对齐,如果要让按键2水平居中且和顶部对齐,需要增加android:layout_centerHorizontal="true",效果图如下:
图5
当然了,如果没有android:layout_alignParentTop="true",也可以达到图5的效果,因为默认就是顶部对齐的,这里只是说明它们的作用而已。
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按键2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
这里尤其要注意android:layout_alignParentBottom和android:layout_alignParentEnd的差别,前者是指底部对齐,后者指末端对齐,如下图:
图6
图6是指按键2的底部和父容器底部对齐。
图7
图7表示按键2和父容器末端对齐,相关的XML控制代码如下:
<Button
android:id="@+id/button2"
android:layout_width="80dp"
android:layout_height="60dp"
android:text="按键2"
android:layout_alignParentEnd="true"
/>
android:layout_centerHorizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInparent 相对于父容器完全居中
(4) 属性值为其他组件ID的属性
android:layout_above本组件在某组件的上方
android:layout_alignBaseline本组件和某组件的基线对齐。
android:layout_alignBottom 本组件的下边缘和某组件的的下边缘对齐
android:layout_alignEnd本组件的末端和某组件末端对齐
android:layout_alignRight 本组件的右边缘和某组件的的右边缘对齐
android:layout_alignLeft本组件左边缘和某组件左边缘对齐
android:layout_alignStart本组件的开始端和某组件开始端对齐
android:layout_alignTop 本组件的顶部和某组件的的顶部对齐
android:layout_below 本组件在某组件的下方
android:layout_toEndOf本组件在某组件末端
android:layout_toLeftOf本组件在某组件的左边
android:layout_toRightOf本组件在某组件的右边
android:layout_alignLeft 本组件在某组件开始端
这里基线概念很重要,参考http://blog.csdn.net/kehrwang/article/details/9041847。
基线解释:书写英语单词时为了规范书写会设有四条线,从上至下第三条就是基线。基线对齐主要是为了两个控件中显示的英文单词的基线对齐。
四、第四组:中心对齐。值为true/false
3. 综合的例子
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/reminder"/>
<Spinner
android:id="@+id/dates"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/times"/>
<Spinner
android:id="@id/times"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_alignParentRight="true"/>
<Button
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/times"
android:layout_alignParentRight="true"
android:text="@string/done"/>
</RelativeLayout>
效果图如下:
图8
参考链接:
Android开发者Relative Layout
http://developer.android.com/guide/topics/ui/layout/relative.html
RelativeLayout相对布局
http://blog.sina.com.cn/s/blog_40797b1001010vwt.html
http://blog.csdn.net/kehrwang/article/details/9041847
Android相对布局(RelativeLayout)的更多相关文章
- Android之布局RelativeLayout
线性布局的weight属性在等比例分配时比较方便,但是对复杂的界面,嵌套多层LinearLayout布局会导致渲染变慢,占用更多系统资源:而使用RelativeLayout的话,可能仅仅需要一层就可以 ...
- android的布局-----RelativeLayout(相对布局)
学习导图 注:父容器定位的属性值只能是Boolean ,兄弟组件定位的属性值只能是ID 典型案例(梅花) <?xml version="1.0" encoding=" ...
- Android UI -- 布局介绍(布局包括FrameLayout, LinearLayout, RelativeLayout, GridLayout)
首先介绍常用布局类 FrameLayout 最简单的布局管理器. 这个布局管理类有几个特性: 添加组件默认在左上角的. 如果添加多个组件会叠加到一起,并且都在左上角.(可以通过一gravity属性改变 ...
- Android 自学之相对布局 RelativeLayout
相对布局(RelativeLayout),相对布局容器内子组件的位置总是相对兄弟组件.父容器来决定的. RelativeLayout的XML属性及相关方法说明 XML属性 相关方法 说明 androi ...
- [Irving]Android 常用布局之RelativeLayout
RelativeLayout相对布局 相对布局 RelativeLayout 允许子元素指定它们相对于其父元素或兄弟元素的位置,这是实际布局中最常用的布局方式之一. 它灵活性大很多,当然属性也多,操作 ...
- 一步一步学android之布局管理器——RelativeLayout
今天开始学习RelativeLayout(相对布局),相对布局在平时布局的时候用的较多,因为Android适配方面的原因.相对布局可以控制组件摆放的位置(放在任一组件的上下左右等位置),下面来看看类的 ...
- Android 五大布局(LinearLayout、FrameLayout、AbsoulteLayout、RelativeLayout、TableLayout )
前言 欢迎大家我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net ...
- android LinearLayout和RelativeLayout实现精确布局
先明确几个概念的区别: padding margin:都是边距的含义,关键问题得明白是什么相对什么的边距padding:是控件的内容相对控件的边缘的边距. margin :是控件边缘相对父空间的边距 ...
- .Net程序猿玩转Android开发---(7)相对布局RelativeLayout
相对布局RelativeLayout是Android布局中一个比較经常使用的控件,使用该控件能够布局出适合各种屏幕分辨率的布局,RelativeLayout採用相对位置进行 ...
随机推荐
- php服务器探针
<?php /* ---------------------------------------------------- */ /* 程序名称: PHP探针-Yahei /* 程序功能: 探测 ...
- Asp.Net MVC使用ViewData导致双引号被转义的解决方法
使用以下方法进行输出 @Html.Raw(ViewData["jsonString"].ToString())
- 在ASP.NET MVC应用程序中实现Server.Transfer()类似的功能
在ASP.NET MVC应用程序中,如果使用Server.Transfer()方法希望将请求转发到其它路径或者Http处理程序进行处理,都会引发“为xxx执行子请求时出错”的HttpException ...
- 【Leetcode】 - Single Number II
Problem Discription: Suppose the array A has n items in which all of the numbers apear 3 times excep ...
- ViewController 优化
解决问题:部分复杂页面的Controller过于庞大,不利于维护与复用: 复杂的页面大多是基于tableview的页面.复杂页面的代码大致可分为两部分(复杂的View布局用Nib实现的话,一般大家都是 ...
- PHP之SQL防注入代码集合(建站常用)
SQL防注入代码一 <?php if (!function_exists (quote)) { function quote($var) { if (strlen($var)) { $var=! ...
- 《head first java 》读书笔记(五)
Updated 2014/04/09 P581--P615 如何组织.包装与部署Java程序. 部署的选择 本机: Executable Jar 两者之间的结合: Web Start, RMI app ...
- POJ 1930
Dead Fraction Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1762 Accepted: 568 Desc ...
- struts2-2.3.4.1的struts-default.xml源码
<?xml version="1.0" encoding="UTF-8" ?> <!-- /* * $Id: struts-default.x ...
- javascript中创建插入元素
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...