Android编程之绝对布局AbsoluteLayout和相对布局RelativeLayout实例详解

本文实例分析了Android编程之绝对布局AbsoluteLayout和相对布局RelativeLayout。分享给大家供大家参考,具体如下:

一、绝对布局AbsoluteLayout

绝对定位AbsoluteLayout,又可以叫做坐标布局,可以直接指定子元素的绝对位置,这种布局简单直接,直观性强,但是由于手机屏幕尺寸差别比较大,使用绝对定位的适应性会比较差。

下面我们举一个例子看看:例子里的机器人图片大小是250X250,可以看到我们使用android:layout_x和android:layout_y来指定子元素的纵横坐标。

XML/HTML代码:

<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout android:id="@+id/AbsoluteLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:background="#fff"><ImageView android:src="@drawable/android" android:layout_y="40dip" android:layout_width="wrap_content" android:layout_x="35dip" android:id="@+id/ImageView01" android:layout_height="wrap_content"> </ImageView> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/TextView01" android:text="Android2.2 学习指南" android:textColor="#0f0" android:textSize="28dip" android:layout_y="330dip" android:layout_x="35dip“> </TextView> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/TextView02" android:text="图文并茂,理论清晰,操作性强" android:textColor="#333" android:textSize="18dip" android:layout_y="365dip" android:layout_x="35dip“> </TextView> </AbsoluteLayout>

让我们看一下在WQVGA的模拟器下的显示效果:

再在WVGA800的模拟器下看看显示效果:

Tip: 在绝对定位中,如果子元素不设置layout_x和layout_y,那么它们的默认值是0,也就是说它会像在FrameLayout一样这个元素会出现在左上角。

二、相对布局RelativeLayout

相对布局 RelativeLayout 允许子元素指定它们相对于其父元素或兄弟元素的位置,这是实际布局中最常用的布局方式之一。它灵活性大很多,当然属性也多,操作难度也大,属性之间产生冲突的的可能性也大,使用相对布局时要多做些测试。

下面我们用相对布局再做一次上面的例子,首先放置一个图片,其它两个文本分别相对上一个元素定位:

XML/HTML代码:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#fff" xmlns:android="http://schemas.android.com/apk/res/android"><ImageView android:id="@+id/ImageView01" android:src="@drawable/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="40dip" > </ImageView> <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/TextView01" android:text="Android2.2 学习指南" android:textColor="#0f0" android:textSize="28dip" android:layout_below="@id/ImageView01" android:layout_centerHorizontal="true" android:layout_marginTop="10dip"> </TextView> <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/TextView02" android:text="图文并茂,理论清晰,操作性强" android:textColor="#333" android:textSize="18dip" android:layout_below="@id/TextView01" android:layout_centerHorizontal="true" android:layout_marginTop="5dip“> </TextView> </RelativeLayout>

让我们看一下在WQVGA的模拟器下的显示效果:

再看一下在更大屏幕(WVGA800)模拟器上的显示效果:

从上图可以看到界面效果基本保持了一致,而不是像绝对定位一样龟缩在左上角;同学们看到自动缩放的功能是采用了dip做单位带来的好处。

下面介绍一下RelativeLayout用到的一些重要的属性:

第一类:属性值为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                      本元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft                      本元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom                 本元素的下边缘和某元素的的下边缘对齐
android:layout_alignRight                    本元素的右边缘和某元素的的右边缘对齐
第三类:属性值为具体的像素值,如30dip,40px
android:layout_marginBottom              离某元素底边缘的距离
android:layout_marginLeft                   离某元素左边缘的距离
android:layout_marginRight                 离某元素右边缘的距离
android:layout_marginTop                   离某元素上边缘的距离

我们再把上面的例子重新做一遍,这一次多放一些属性在里面,大家试验一下:

XML/HTML代码:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#cfff" 色彩的设置是argb,第一个c是透明度 xmlns:android="http://schemas.android.com/apk/res/android"> <ImageView android:id="@+id/ImageView01" android:src="@drawable/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="40dip" android:layout_centerHorizontal="true"> </ImageView><TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/TextView01" android:text="Android2.2 学习指南" android:textColor="#0f0" android:textSize="28dip" android:layout_below="@id/ImageView01" android:layout_centerHorizontal="true" android:layout_marginTop="10dip"> </TextView><TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/TextView02" android:text="图文并茂,理论清晰,操作性强" android:textColor="#333" android:textSize="18dip" android:layout_below="@id/TextView01" android:layout_centerHorizontal="true" android:layout_marginTop="5dip"> </TextView> <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/TextView03" android:text="alignTop" android:textColor="#333" android:textSize="18dip" android:layout_alignTop="@id/ImageView01" 和ImageView01上边缘对齐 android:layout_centerHorizontal="true"> </TextView><TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/TextView04" android:text="alignLeft" android:textColor="#333" android:textSize="18dip" android:layout_alignLeft="@id/ImageView01" android:layout_centerHorizontal="true"> </TextView><TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/TextView05" android:text="alignRight" android:textColor="#333" android:textSize="18dip" android:layout_alignRight="@id/ImageView01" android:layout_centerHorizontal="true"> </TextView><TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/TextView06" android:text="alignBottom" android:textColor="#333" android:textSize="18dip" android:layout_alignBottom="@id/ImageView01" android:layout_centerHorizontal="true"> </TextView> </RelativeLayout>

绝对布局AbsoluteLayout和相对布局RelativeLayout的内容就讲完了,希望本文所述对大家Android程序设计有所帮助。

时间: 2024-07-30 05:08:09

Android编程之绝对布局AbsoluteLayout和相对布局RelativeLayout实例详解的相关文章

Android编程之基于Log演示一个activity生命周期实例详解

本文实例讲述了Android编程之基于Log演示一个activity生命周期.分享给大家供大家参考,具体如下: 利用Android的Log 演示一个activity的生命周期 代码: //DemoActivity.java package uni.activity; /* @author octobershiner 2011 7 22 SE.HIT */ import android.app.Activity; import android.os.Bundle; import android.u

Android ListView里控件添加监听方法的实例详解

Android ListView里控件添加监听方法的实例详解 关于ListView,算是android中比较常见的控件,在ListView我们通常需要一个模板,这个模板指的不是住模块,而是配置显示在ListView里面的东西,今天做项目的时候发现想要添加一个ImageView监听方法,发现崩了,也许是好久没有动ListView竟然忘了不能直接在主UI的xml文件里面调用其他xml文件的控件,哪怕ListView用的是这个xml文件. [错误示范]: 直接调用ImageView这个控件是ListV

修改Android FloatingActionButton的title的文字颜色及背景颜色实例详解

修改Android FloatingActionButton的title的文字颜色及背景颜色实例详解 首先看一张图片 我是在一个不错的开源的FloatingActionButton库基础上实现的,链接github开源库 参考图片的标记和代码里的注释.代码如下: <com.getbase.floatingactionbutton.FloatingActionsMenu android:id="@+id/fab_meau" android:layout_width="wra

Android判断后台服务是否开启的两种方法实例详解

Android判断后台服务是否开启的两种方法实例详解 最近项目用到后台上传,就开启了一个服务service. 但是刚开始用这种方法,有些机型不支持:酷派不支持.然后又换了第二种判断方法. // public boolean isServiceWork(Context mContext, String serviceName) { // boolean isWork = false; // ActivityManager myAM = (ActivityManager) mContext // .

Android编程之绝对布局AbsoluteLayout和相对布局RelativeLayout实例详解_Android

本文实例分析了Android编程之绝对布局AbsoluteLayout和相对布局RelativeLayout.分享给大家供大家参考,具体如下:  一.绝对布局AbsoluteLayout 绝对定位AbsoluteLayout,又可以叫做坐标布局,可以直接指定子元素的绝对位置,这种布局简单直接,直观性强,但是由于手机屏幕尺寸差别比较大,使用绝对定位的适应性会比较差. 下面我们举一个例子看看:例子里的机器人图片大小是250X250,可以看到我们使用android:layout_x和android:l

Android GZip的使用-开发中网络请求的压缩实例详解_Android

Android  GZip: gzip是GNUzip的缩写,它是一个GNU自由软件的文件压缩程序. HTTP协议上的GZIP编码是一种用来改进WEB应用程序性能的技术.一般服务器中都安装有这个功能模块的,服务器端不需做改动. 当浏览器支持gzip 格式的时候, 服务器端会传输gzip格式的数据. 从Http 技术细节上讲,就是 http request 头中 有 "Accept-Encoding", "gzip" ,response 中就有返回头Content-En

Android编程实现XML解析与保存的三种方法详解

本文实例讲述了Android编程实现XML解析与保存的三种方法.分享给大家供大家参考,具体如下: 简介 在Android开发中,关于XML解析有三种方式,分别是: 1. SAX 基于事件的解析器,解析速度快,占用内存少.非常适合在Android移动设备中使用. 2. DOM 在内存中以树形结构存放,因此检索和更新效率会更高.但是对于特别大的文档,解析和加载整个文档将会很耗资源 3. PULL 基于事件的解析器,不同于SAX是,PULL是主动请求下一个事件,所以在可控上PULL要比SAX实用.An

jqm的多列布局demo,html5的多列布局demo,多列布局的详细讲解,html5开发实例详解

由于移动设备屏幕宽度较小,所以一般不建议使用多列布局.但有时你可能需要并排放置一些元素(如按钮之类的).  jQuery Mobile通过约定的类名ui-grid来提供了一种基于css的多列布局方法.共提供了五种布局方式,分别是:ui-gird-solo:单列布局,ui-gird-a:双列布局,ui-gird-b:三列布局,ui-gird-c:四列布局,ui-gird-d:五列布局.布局网格总宽度100%,完全不可见(无边框无背景),没有任何内边距或外边距,不会对网格中的内容产生任何干扰. 今天

Android中系统自带锁WalkLock与KeyguardLock用法实例详解

本文实例讲述了Android中系统自带锁WalkLock与KeyguardLock用法.分享给大家供大家参考,具体如下: WalkLock - 顾名思义 唤醒锁 点亮屏幕用的 KeyguardLock - 顾名思义 键盘锁 解锁键盘用的 详细介绍: 1: WalkLock 唤醒锁 - WalkLock真的能点亮屏幕吗? 答案是肯定的. 可是有时候为什么不点亮屏幕,这个就是参数设置的问题了. 复制代码 代码如下:PowerManager.newWakeLock(PowerManager.FULL_