`

相对布局简解

 
阅读更多

Android布局之RelativeLayout(相对布局)

相对布局由于属性比较多,所以用起来有些费力,但灵活性较其他布局方法好,所以掌握好相对布局将会非常有用。先看等一个例子:

<?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"> <TextView android:id="@+id/label" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Type here:"/> <EditText android:id="@+id/entry" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@android:drawable/editbox_background" android:layout_below="@id/label"/> <Button android:id="@+id/ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/entry" android:layout_alignParentRight="true" android:layout_marginLeft="10dip" android:text="OK" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/ok" android:layout_alignTop="@id/ok" android:text="Cancel" /></RelativeLayout>
这是很常见的布局内容,讲解如下:

android:layout_below="@id/label"/>
将当前控件放置于id为label 的控件下方。

android:layout_alignParentRight="true"
使当前控件的右端和父控件的右端对齐。这里属性值只能为true或false,默认false。

android:layout_marginLeft="10dip"
使当前控件左边空出相应的空间。

android:layout_toLeftOf="@id/ok"
使当前控件置于id为ok的控件的左边。

android:layout_alignTop="@id/ok"
使当前控件与id控件的上端对齐。

至此,我们已经发现,其属性之繁多。下面简单归纳一下:
第一类:属性值为true或false
*android:layout_centerHrizontal
*android:layout_centerVertical
*android:layout_centerInparent
*android:layout_alignParentBottom
*android:layout_alignParentLeft
*android:layout_alignParentRight
*android:layout_alignParentTop
*android:layout_alignWithParentIfMissing
第二类:属性值必须为id的引用名“@id/id-name
*android:layout_below
*android:layout_above
*android:layout_toLeftOf
*android:layout_toRightOf
*android:layout_alignTop
第三类:属性值为具体的像素值,如30dip,40px
*android:layout_marginBottom
*android:layout_marginLeft
*android:layout_marginRight
*android:layout_marginTop

再看第二个例子:

<?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"> <AnalogClock android:id="@+id/aclock" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true"/> <DigitalClock android:id="@+id/dclock" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/aclock" android:layout_alignLeft="@id/aclock" android:layout_marginLeft="40px"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="当前时间:" android:layout_toLeftOf="@id/dclock" android:layout_alignTop="@id/aclock"/> </RelativeLayout>

分享到:
评论

相关推荐

    Android 布局简介

    RelativeLayout(相对布局) 简介 属性 栗子 TableLayout(表格布局) 简介 属性  栗子 绝对布局(帧布局) 简介 ConstraintLayout(约束布局) 简介 demo地址    序   近来很多人问了我关于Android布局相关的...

    跨平台布局组件FMXUI.zip

    【类介绍】 TView 基本视图 TViewGroup 视图组 TLinearLayout 线性布局 TRelativeLayout 相对布局 TGridsLayout 格子布局 TTextView 文本视图 TButtonView 按钮视图 TEditView 编辑框视图 TProgressView ...

    CSS网页布局教程:绝对定位和相对定位

    网页制作Webjx文章简介:本文主要描述XHTML中相对定位和绝对定位各自的本质、用法、区别和两者之间的关系。以及使用CSS的Left、Right、Top、Bottom属性(偏移属性)和Margin属性(外边距)对定位块级元素进行布局的...

    Android核心技术与实例详解 PDF扫描版

    3 4 2 相对布局案例 3 5 帧布局 3 5 1 FrameLayout类简介 3 5 2 帧布局案例 3 6 绝对布局 3 6 1 AbsoluteLayout类简介 3 6 2 绝对布局案例 3 7 小结 第4章 Android常用基本控件 4 1 文本控件的介绍 4 1 1 TextView类...

    六大布局之FrameLayout的使用

    这一期我们为大家讲解一下FrameLayout(帧布局)的使用,相较于其他布局,FrameLayout可以说的上是最简单的一个,并且其使用范围相对来说也相对较小,但是也是Android中的六大布局之一,面试的时候还是会碰到的,...

    Android开发与应用——张荣,原书配套课件

    5.1.4 相对布局 5.1.5 绝对布局 5.1.6 复用XML布局文件 5.2 控制视图界面的其他方法 5.2.1 代码控制视图界面 5.2.2 代码和XML联合控制视图界面 5.3 多界面的使用 5.3.1 使用Intent封装数据 5.3.2 ...

    Android使用addView动态添加组件的方法

    其中,添加的布局主要有RelativeLayout型(相对布局)的和LinearLayout(线性布局) 添加的组件主要有文本显示框,编辑框,按钮等组件。  下面,就让我们来进行实现: 首先我们创建一个新的项目,删除...

    Android基础知识详解

    RelativeLayout(相对布局) 31 AbsoluteLayout(绝对布局) 34 FrameLayout(框架布局) 34 Toast 36 Notification 38 对话框 42 一、带三个按钮的对话框 42 二、简单列表对话框、单选列表对话框、多选列表对话框 44 三、...

    android学习文档

    2.4 相对布局 23 2.5 基本界面组件 23 2、Activity生命周期及其实例分析 24 1.1 Avtivity介绍 24 1.2 Activity的生命周期 25 1.2.1 Activity的三种状态 25 1.2.2 Activity在三种状态切换时调用的方法(7种) 25 1.2.3 ...

    Web前端大作业2.0.zip

    接下来拉到下面的国外四位棋手简介可以发现这是树形布局的四个div框,依照树形结构错落有致排开,分别展示上面四位棋手对应的资料简介,以及人物生平获奖资料等。网站靠近底部的位置就是国内群雄争霸比赛报名的Form...

    AI领跑者:百度智能交通业务简介

    介绍了百度智能交通业务的战略路径、战略布局以及应用场景和强大的数字底座。适用于想了解百度智能交通业务的朋友,以及相对智能交通领域有整体认知的朋友。

    Android入门到精通源代码.

    6.2.3 相对布局(RelativeLayout) 6.2.4 表格布局(TableLayout) 6.2.5 绝对布局(AbsoluteLayout) 6.3 界面中的字体 6.3.1 设置系统字体 6.3.2 引用用户自定义字体 6.4 应用实例详解:制作手机桌面 6.4.1 实例...

    [东莞]某欧式风格33层住宅楼建筑方案图(含效果图)

    小区内静外动,商业规划侧重城市性,设置集中商业和特色风情步行街,体现社区配套设施的城市共享性及经营性,社区商业及公共设施位于城市界面沿线,具有相对的独立性,同时亦成为城市共享的一部分,在为小区居民...

    flutter-guide::fire::fire::fire:学习flutter,各种flutter控件的基本使用和动画效果,简单易学。flutter 入门与进阶http

    Flutter入门与进阶入门第二章 基础组件2.1: widget 简介2.2:状态管理2.3:文本和样式2.4:按钮2.5:图片和Icon2.6:单选和复选矿2.7:输入框2.8: 进度指示器2.9: 弹窗第三章 布局3.1 布局类组件简介3.2 线性布局...

    《CSS设计彻底研究》光盘源码

     4.2.2 relative(相对定位)   4.2.3 absolute(绝对定位)   4.2.4 fixed(固定定位)   4.3 z-index空间位置   4.4 盒子的display属性   4.5 本章小结  第5章 文字与图像  …… 第 6章 链接...

    css设计彻底研究 源代码

     2.1 “CSS禅意花园”简介 2.2 郊野——两列布局 2.3 像素画——三列布局 2.4 百合池塘——三列布局变体 2.5 郁金香——多列布局 2.6 日与夜——包含圆角的设计 2.7 Si6——包含倾斜的设计 2.8 清茶...

    微信小程序-第一个微信小程序demo

    3、require()引入js时,不能使用绝对路径,只能使用相对路径,import引入文件时,相对绝对都可以; 4、wxml最外层是有个page标签的,编辑看不到,调试的时候可以看到,必要的时候可以对这个标签设置样式; 小程序...

Global site tag (gtag.js) - Google Analytics