Android  AbsoluteLayout和RelativeLayout布局详解

 更新时间:2016年08月08日 16:14:38   作者:chino  
本文主要讲解Android AbsoluteLayout和RelativeLayout布局,这里整理了相关资料,并附示例代码和效果图,有兴趣的小伙伴可以参考下

Android 线性布局: AbsoluteLayout布局和RelativeLayout布局。

 1、绝对布局 AbsoluteLayout

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

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

<?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一样这个元素会出现在左上角。

2、相对布局 RelativeLayout

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

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

<?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做单位带来的好处。关于dip,不懂的同学可以看我在开发小知识里写的专门的文章。

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

第一类:属性值为true或false

  1. android:layout_centerHrizontal                                           水平居中
  2. android:layout_centerVertical                                            垂直居中
  3. android:layout_centerInparent                                           相对于父元素完全居中
  4. android:layout_alignParentBottom                                     贴紧父元素的下边缘
  5. android:layout_alignParentLeft                                          贴紧父元素的左边缘
  6. android:layout_alignParentRight                                        贴紧父元素的右边缘
  7. android:layout_alignParentTop                                          贴紧父元素的上边缘
  8. android:layout_alignWithParentIfMissing                            如果对应的兄弟元素找不到的话就以父元素做参照物

第二类:属性值必须为id的引用名“@id/id-name

  1. android:layout_below                          在某元素的下方
  2. android:layout_above                          在某元素的的上方
  3. android:layout_toLeftOf                       在某元素的左边
  4. android:layout_toRightOf                     在某元素的右边
  5. android:layout_alignTop                      本元素的上边缘和某元素的的上边缘对齐
  6. android:layout_alignLeft                      本元素的左边缘和某元素的的左边缘对齐
  7. android:layout_alignBottom                 本元素的下边缘和某元素的的下边缘对齐
  8. android:layout_alignRight                    本元素的右边缘和某元素的的右边缘对齐

第三类:属性值为具体的像素值,如30dip,40px

  1. android:layout_marginBottom              离某元素底边缘的距离
  2. android:layout_marginLeft                   离某元素左边缘的距离
  3. android:layout_marginRight                 离某元素右边缘的距离
  4. android:layout_marginTop                   离某元素上边缘的距离

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

<?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>

以上就是对Android AbsoluteLayout和RelativeLayout布局的介绍,后续继续整理相关资料,谢谢大家对本站的支持!

相关文章

  • Android数字选择器NumberPicker使用详解

    Android数字选择器NumberPicker使用详解

    这篇文章主要为大家详细介绍了Android数字选择器NumberPicker的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-08-08
  • Android自定义View模仿QQ讨论组头像效果

    Android自定义View模仿QQ讨论组头像效果

    最近发现QQ讨论组的头像非常不错,正好最近又有时间,所有就动手实践了下,所以下面这篇文章主要给大家介绍了Android自定义View模仿QQ讨论组头像效果的相关资料,文中给出了详细的介绍的示例代码,需要的朋友可以参考学习,下面来一起看看吧。
    2017-04-04
  • 5个Android开发中比较常见的内存泄漏问题及解决办法

    5个Android开发中比较常见的内存泄漏问题及解决办法

    本文主要介绍了5个Android开发中比较常见的内存泄漏问题及解决办法,具有很好的参考价值,下面跟着小编一起来看下吧
    2017-02-02
  • Android利用爬虫实现模拟登录的实现实例

    Android利用爬虫实现模拟登录的实现实例

    这篇文章主要介绍了Android利用爬虫实现模拟登录的实现实例的相关资料,希望通过本文能帮助到大家实现这样的功能,需要的朋友可以参考下
    2017-09-09
  • DatePicker日期滚动选择使用详解

    DatePicker日期滚动选择使用详解

    这篇文章主要为大家详细介绍了DatePicker日期滚动选择的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-09-09
  • Android Socket服务端与客户端用字符串的方式互相传递图片的方法

    Android Socket服务端与客户端用字符串的方式互相传递图片的方法

    这篇文章主要介绍了Android Socket服务端与客户端用字符串的方式互相传递图片的方法的相关资料,需要的朋友可以参考下
    2016-05-05
  • Android实现侧滑只需一步

    Android实现侧滑只需一步

    这篇文章主要介绍了Android实现侧滑只需一步,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • 解决android关于打开虚拟机时右侧工具栏不显示的问题

    解决android关于打开虚拟机时右侧工具栏不显示的问题

    下面小编就为大家分享一篇解决android关于打开虚拟机时右侧工具栏不显示的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-01-01
  • Android实现仿通讯录侧边栏滑动SiderBar效果代码

    Android实现仿通讯录侧边栏滑动SiderBar效果代码

    这篇文章主要介绍了Android实现仿通讯录侧边栏滑动SiderBar效果代码,实例分析了通讯录侧边栏滑动效果的实现技巧,并附带完整实例代码供读者下载参考,需要的朋友可以参考下
    2015-10-10
  • 获取android4.0版本sdcard路径示例

    获取android4.0版本sdcard路径示例

    自从android4.0开始,谷歌为没有内存卡的手机模拟了一个SD卡,占用了原来的SD卡路径,并为真实的sd卡挂载到该目录的子目录,由于所挂载的目录并没有官方规范,所以命名会不同,只能通过搜索,下面是获取android4.0版本sdcard路径示例
    2014-03-03

最新评论