`

RelativeLayout的理解

阅读更多
一、概述

   在开发中推荐尽量使用RelativeLayout可以增强其适应不同屏幕的灵活性。该布局根据widgets之间关系和widget与父容器之间的关系来确定布局。例如可以设置A在父容器的底部对齐,B在A的上方。

   设置RelativeLayout的两大类主要的属性,如下:

   1.widget与所在容器(Container)的关系
      android:layout_alignParentTop
      android:layout_alignParentBottom
      android:layout_alignParentLeft
      android:layout_centerHorizontal
      ......

   2.widgets之间关系
     android:layout_above
     android:layout_below
     android:layout_toLeft
     android:layout_toRight
     android:layout_alignTop 
     ......

   注意:widgets的出现先后顺序值得注意。被参照的widget必须保证已经出现了的。
二、实例分析
【效果图】


【代码要点】

main.xml

view plaincopy to clipboardprint?

   1. <?xml version="1.0" encoding="utf-8"?> 
   2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
   3.     android:orientation="vertical" 
   4.     android:layout_width="fill_parent" 
   5.     android:layout_height="wrap_content" 
   6.     android:padding="5px" 
   7.     > 
   8. <TextView   
   9.     android:id="@+id/label" 
  10.     android:layout_width="wrap_content"  
  11.     android:layout_height="wrap_content"  
  12.     android:text="URL:" 
  13.     android:paddingTop="10px" 
  14.     /> 
  15. <EditText 
  16.     android:id="@+id/entry" 
  17.     android:layout_width="fill_parent"  
  18.     android:layout_height="wrap_content"  
  19.     android:layout_toRightOf="@id/label" 
  20. /> 
  21. <Button  
  22.     android:id="@+id/button1"  
  23.     android:layout_width="wrap_content" 
  24.     android:layout_height="wrap_content" 
  25.     android:text="OK"  
  26.     android:layout_below="@id/entry"  
  27.     android:layout_alignRight="@id/entry" 
  28.     /> 
  29. <Button  
  30.     android:id="@+id/button2"  
  31.     android:layout_width="wrap_content"  
  32.     android:layout_height="wrap_content"  
  33.     android:text="Cancel"  
  34.     android:layout_toLeftOf="@+id/button1"  
  35.     android:layout_alignBottom="@+id/button1" 
  36.     /> 
  37. </RelativeLayout> 

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5px" > <TextView android:id="@+id/label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="URL:" android:paddingTop="10px" /> <EditText android:id="@+id/entry" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/label" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="OK" android:layout_below="@id/entry" android:layout_alignRight="@id/entry" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cancel" android:layout_toLeftOf="@+id/button1" android:layout_alignBottom="@+id/button1" /> </RelativeLayout> 
三、margin和padding的区别

  • 大小: 22.4 KB
  • 大小: 20 KB
1
0
分享到:
评论

相关推荐

    RelativeLayout布局入门实例

    Android布局入门,LinearLayout和RelativeLayout的理解和应用

    RelativeLayout

    Android 开发入门 对 RelativeLayout的基本属性的讲解和用法 ,更容易的帮你理解和使用

    android相对布局和绝对布局

    这个程序写了对相对布局和绝对布局的例子,帮助大家理解两者的使用和区别。

    Android clipChildren属性实例详解

    本文主要介绍Android clipChildren的属性,这里对clipChildren属性做了一个小例子,展示了效果图和实例代码,方便大家观看理解

    TwelveCustomViews:收集网上的示例,练习理解自定义视图和视图容器类

    4)实现对Android布局LinearLayout,FrameLayout,RelativeLayout,TableLayout的自定义封装; 2015-4-8 说明:上面计划的是对Android的控件和布局进行自定义封装,在阅读API和源码的过程中发现,对他们在Android...

    Android ScrollView无法填充满屏幕的解决办法

    以下代码在Scrollview里面放了一个RelativeLayout、并且是设置为Android:layout_height=”match_parent”填充全屏的和RelativeLayout里面放置了一个TextView背景设为了一张图片、按照代码理解、图片应该是居于屏幕的...

    ConstraintLayoutDemo:Android 约束布局示例

    ConstraintLayout有效的解决了多层嵌套复杂布局的问题,加快渲染速度,有点类似与RelativeLayout和LinearLayout的集合,但是远比RelativeLayout和LinearLayout强大。 本文建议的是通过手写代码的方式来构造布局,等...

    自己开发测试平台,多个控件的综合运用

    我在开发项目模块中使用的测试平台,主要用来测试一下组件的属性,这个平台中综合运用了象 FrameLayout relativelayout linearlayout 这些组件,在一个main.xml中同时用到,并明显的区别了它们的使用效果。...

    Android View事件分发机制详解

    一个应用的布局是丰富的,有TextView,ImageView,Button等,这些子View的外层还有ViewGroup,如RelativeLayout,LinearLayout。作为一个开发者,我们会思考,当点击一个按钮,Android系统是怎样确定我点的就是按钮...

    水牛01248:flex布局学习笔记

    经典教程 Flex 布局教程:语法篇 Flex 布局教程:实例篇 flex布局游戏 理解 flex布局实现需要至少两层...不像安卓里有那么多的定义好了布局规则的ViewGroup,比如RelativeLayout,Linearlayout,以及Listview,GridVi

    Android开发简易音乐播放器

    但是,由于这是一个简易版的音乐播放器,所播放的音乐只有一首,且被写死,但,操作却十分简单,方便理解! 这是代码的主要设计: 音乐主要存放在这一个文件中: 下面就来介绍各部分代码: activity—main。xml: ...

    view的几种布局方式及实践

    我们对Android应用程序运行原理及布局文件可谓有了比较深刻的认识和理解,并且用“HelloWorld!”程序来实践证明了。在继续深入Android开发之旅之前,有必要解决前两篇中没有介绍的遗留问题:View的几种布局显示方法...

    Android编程实现可滑动的开关效果(附demo源码下载)

    直接把自定义的这个View代码放上来,有注释应该很好理解: 首先是布局: &lt;?xml version=1.0 encoding=utf-8?&gt; &lt;RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:id=@+...

    Android手机开发 使用线性布局和相对布局实现Button垂直水平居中

    本文主要结合自己的理解分别对使用LinearLayout和RelativeLayout两种方式实现居中做了总结,希望对大家有所帮助。

    利用MVC编写广告条轮播效果

    我理解的安卓中MVC思想是利用Holder进行编程,展示布局,提供视图,将视图和数据进行绑定起来,在很多App中我们能见到广告条,广告条这里我们可以使用FramLayout进行填充,然后利用MVC的思想将提供的布局添加到...

    Android.UI基础教程]

    2.4.1 ListActivity 2.4.2XML布局 2.4.3 把数据绑定到列表 2.4.4 Loader 2.5 理解活动 2.5.1 声明活动 2.5.2了解活动的生命周期 2.5.3 了解任务和返回堆栈 2.5.4 处理配置更改 2.6 防止应用程序无响应(ANR) 2.6.1...

    android开发——简易计算器的设计报告.doc

    二、实训任务 1、Activity的相关知识 简单理解Activity 代表一个用户所能看到的屏幕,Activity 主要是处理一个应用的整体性工作。Activity是最基本的Android 应用程序组件,应用程序中,一个活动通常就是一个单独的...

Global site tag (gtag.js) - Google Analytics