New UI-布局之RelativeLayout(相对布局)详解

来源:互联网 发布:淘宝怎么来的 编辑:程序博客网 时间:2024/06/06 18:59

New UI-布局之RelativeLayout(相对布局)详解

 ——转载请注明出处:coder-pig,欢迎转载,请勿用于商业用途!


小猪Android开发交流群已建立,欢迎大家加入,无论是新手,菜鸟,大神都可以,小猪一个人的

力量毕竟是有限的,写出来的东西肯定会有很多纰漏不足,欢迎大家指出,集思广益,让小猪的博文

更加的详尽,帮到更多的人,O(∩_∩)O谢谢!

小猪Android开发交流群:小猪Android开发交流群群号:421858269

新Android UI实例大全目录:http://blog.csdn.net/coder_pig/article/details/42145907


本节引言:

在上一节中我们对LinearLayout进行了详细的解析,谢谢大家的热情反馈,大笑LinearLayout也是我们

用的比较多的一个布局,我们更多的时候更钟情于他的weight(权重)属性,等比例划分,对屏幕适配还是

帮助蛮大的;但是使用LinearLayout的时候也有一个问题,就是当界面比较复杂的时候,需要嵌套多层的

LinearLayout,这样就会降低UI Render的效率(渲染速度),而且如果是listview或者GridView上的

item,效率会更低,另外太多层LinearLayout嵌套会占用更多的系统资源,还有可能引发stackoverflow;

但是如果我们使用RelativeLayout的话,可能仅仅需要一层就可以完成了,以父容器或者兄弟组件+margin

+padding就可以设置组件的显示位置,是比较方便的!当然,也不是绝对的,具体问题具体分析吧!

总结就是:尽量使用RelativeLayout + LinearLayout的weight属性搭配使用吧!



1.本节核心图:


2.父容器定位属性的示意图:



3.根据兄弟组件定位

恩,先说下什么是兄弟组件吧,所谓的兄弟组件就是处于同一层次容器的组件,如图


图中的组件1,2就是兄弟组件了,而组件3与组件1或组件2并不是兄弟组件,所以组件3不能通过

组件1或2来进行定位,比如layout_toleftof = "组件1"这样是会报错的!切记!

关于这个兄弟组件定位的最经典例子就是"梅花布局"了,下面代码实现下:

运行效果图:


实现代码:

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  2.     xmlns:tools="http://schemas.android.com/tools"    
  3.     android:id="@+id/RelativeLayout1"    
  4.     android:layout_width="match_parent"    
  5.     android:layout_height="match_parent" >    
  6.     
  7.     <!-- 这个是在容器中央的 -->    
  8.         
  9.     <ImageView    
  10.         android:id="@+id/img1"     
  11.         android:layout_width="80dp"    
  12.         android:layout_height="80dp"    
  13.         android:layout_centerInParent="true"    
  14.         android:src="@drawable/pic1"        
  15.     />    
  16.         
  17.     <!-- 在中间图片的左边 -->    
  18.     <ImageView    
  19.         android:id="@+id/img2"     
  20.         android:layout_width="80dp"    
  21.         android:layout_height="80dp"    
  22.         android:layout_toLeftOf="@id/img1"    
  23.         android:layout_centerVertical="true"    
  24.         android:src="@drawable/pic2"        
  25.     />    
  26.         
  27.     <!-- 在中间图片的右边 -->    
  28.     <ImageView    
  29.         android:id="@+id/img3"     
  30.         android:layout_width="80dp"    
  31.         android:layout_height="80dp"    
  32.         android:layout_toRightOf="@id/img1"    
  33.         android:layout_centerVertical="true"    
  34.         android:src="@drawable/pic3"        
  35.     />    
  36.         
  37.     <!-- 在中间图片的上面-->    
  38.     <ImageView    
  39.         android:id="@+id/img4"     
  40.         android:layout_width="80dp"    
  41.         android:layout_height="80dp"    
  42.         android:layout_above="@id/img1"    
  43.         android:layout_centerHorizontal="true"    
  44.         android:src="@drawable/pic4"        
  45.     />    
  46.         
  47.     <!-- 在中间图片的下面 -->    
  48.     <ImageView    
  49.         android:id="@+id/img5"     
  50.         android:layout_width="80dp"    
  51.         android:layout_height="80dp"    
  52.         android:layout_below="@id/img1"    
  53.         android:layout_centerHorizontal="true"    
  54.         android:src="@drawable/pic5"        
  55.     />    
  56.     
  57. </RelativeLayout>    


4.margin与padding的区别:

初学者对于这两个属性可能会有一点混淆,这里区分下:

首先margin代表的是偏移,比如marginleft = "5dp"表示组件离容器左边缘偏移5dp;

而padding代表的则是填充,而填充的对象针对的是组件中的元素,比如TextView中的文字

比如为TextView设置paddingleft = "5dp",则是在组件里的元素的左边填充5dp的空间!

margin针对的是容器中的组件,而padding针对的是组件中的元素,要区分开来!

下面通过简单的代码演示两者的区别:

代码如下:

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  2.     xmlns:tools="http://schemas.android.com/tools"    
  3.     android:layout_width="match_parent"    
  4.     android:layout_height="match_parent"    
  5.     android:paddingBottom="@dimen/activity_vertical_margin"    
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"    
  7.     android:paddingRight="@dimen/activity_horizontal_margin"    
  8.     android:paddingTop="@dimen/activity_vertical_margin"    
  9.     tools:context=".MainActivity" >    
  10.     
  11.     <Button    
  12.         android:id="@+id/btn1"     
  13.         android:layout_height="wrap_content"    
  14.         android:layout_width="wrap_content"    
  15.         android:text="Button"        
  16.     />    
  17.     <Button    
  18.         android:paddingLeft="100dp"     
  19.         android:layout_height="wrap_content"    
  20.         android:layout_width="wrap_content"    
  21.         android:text="Button"    
  22.         android:layout_toRightOf="@id/btn1"        
  23.     />    
  24.         
  25.         
  26.         
  27.         
  28.     <Button    
  29.         android:id="@+id/btn2"     
  30.         android:layout_height="wrap_content"    
  31.         android:layout_width="wrap_content"    
  32.         android:text="Button"    
  33.         android:layout_alignParentBottom="true"        
  34.     />    
  35.     <Button    
  36.         android:layout_marginLeft="100dp"     
  37.         android:layout_height="wrap_content"    
  38.         android:layout_width="wrap_content"    
  39.         android:text="Button"    
  40.         android:layout_toRightOf="@id/btn2"     
  41.         android:layout_alignParentBottom="true"       
  42.     />    
  43.         
  44. </RelativeLayout>  
运行效果图:


相信大家看效果图就可以知道这两个的区别了!



5.很常用的一点:margin可以设置为负数

相信很多朋友都不知道一点吧,平时我们设置margin的时候都习惯了是正数的,

其实是可以用负数的,下面写个简单的程序演示下吧,模拟进入软件后,弹出广告

页面的,右上角的cancle按钮的margin则是使用负数的!

效果图:


贴出的广告Activity的布局代码吧,当然,如果你对这个有兴趣的话可以下下demo,

因为仅仅是实现效果,所以代码可能会有点粗糙!

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context="com.jay.example.relativelayoutdemo.MainActivity"   
  6.     android:background="#00CCCCFF">  
  7.   
  8.     <ImageView  
  9.         android:id="@+id/imgBack"  
  10.         android:layout_width="200dp"  
  11.         android:layout_height="200dp"  
  12.         android:layout_centerInParent="true"  
  13.         android:background="@drawable/myicon" />  
  14.   
  15.     <ImageView  
  16.         android:id="@+id/imgCancle"  
  17.         android:layout_width="28dp"  
  18.         android:layout_height="28dp"  
  19.         android:layout_alignRight="@id/imgBack"  
  20.         android:layout_alignTop="@id/imgBack"  
  21.         android:background="@drawable/cancel"  
  22.         android:layout_marginTop="-15dp"  
  23.         android:layout_marginRight="-10dp" />  
  24.   
  25. </RelativeLayout>  


得意恩,那么本节关于RelativeLayout的详细解答就到这里,如果发现有什么遗漏,或者好玩的

笔者一定mark下来,分享给大家,O(∩_∩)O谢谢!



RelativeLayoutDemo代码下载:http://download.csdn.net/detail/zpj779878443/833


0 0
原创粉丝点击