详解Android Widget组件RelativeLayout实例

转载自:http://mobile.51cto.com/widget-246471.htm

 

以前很少用RelativeLayout,但是这次笔者的项目用到了RelativeLayout。用起来才发现RelativeLayout太灵活了。很容易给人造成一些错误。

下面谈谈笔者的看法。

引用

 
 
  1. From Tutorials:  
  2.  
  3. If you find yourself using several nested LinearLayout groups, you may be able toreplace them with a single RelativeLayout  
  4.  

以上来自Tutorials,笔者理解的观点是,当有过个ViewGroup嵌套的时候,再去考虑用RelativeLayout,笔者觉得既然官方这么写,很程度是因为,RelativeLayout太灵活了,它的灵活性给我们对UI的控制多少回造成一定影响。

曾经有人跟笔者说过,RelativeLayout跟FrameLayout有一些相似,给人的感觉是分层的。有层的这个概念。
笔者觉得不是这样的,是没有层的概念的。从官方的解释上可以看出这东西就是可以设置相对布局的一个布局而已。没有层的概念。

先上段代码,更直观的看看。

Java代码

 
 
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <RelativeLayout    
  3.     xmlns:android="http://schemas.android.com/apk/res/android"    
  4.     android:layout_width="fill_parent"    
  5.     android:layout_height="fill_parent"    
  6.     android:background="#CCFFFF">    
  7.     <LinearLayout    
  8.         android:id="@+id/linearLayout"    
  9.         android:layout_width="fill_parent"    
  10.         android:layout_height="200dp"    
  11.         android:background="#32000033"    
  12.         android:orientation="vertical">    
  13.         <Button    
  14.             android:id="@+id/button1"    
  15.             android:layout_width="fill_parent"    
  16.             android:layout_height="wrap_content"    
  17.             android:background="#FF3300"    
  18.             android:text="Button" />    
  19.         <TextView    
  20.             android:id="@+id/textView"    
  21.             android:layout_width="fill_parent"    
  22.             android:layout_height="wrap_content"    
  23.             android:layout_weight="1"    
  24.             android:text="Base"    
  25.             android:textColor="#6633FF"    
  26.             android:gravity="center" />    
  27.         <Button    
  28.             android:id="@+id/button2"    
  29.             android:layout_width="fill_parent"    
  30.             android:layout_height="wrap_content"    
  31.             android:background="#FF3300"    
  32.             android:text="Button" />    
  33.     </LinearLayout>    
  34.     <Button    
  35.         android:id="@+id/button3"    
  36.         android:layout_width="100dp"    
  37.         android:layout_height="50dp"    
  38.         android:layout_centerInParent="true"    
  39.         android:layout_alignBottom="@id/linearLayout"    
  40.         android:text="button" />    
  41. </RelativeLayout>   

只贴xml,activity没什么东西,就显示一下罢了。

运行效果图

运行效果图

很明显可以看出button3的下边缘是跟lineLayout的下边缘在一条水平线上的。

Java代码
 

 
 
  1. android:layout_alignBottom="@id/button1"    

但是当像上面一样设置的时候,我们可能会是想让button3的下边缘跟button1的下边缘在一个水平线,但是这些写的效果却不是按我们所想的显示,如此设置根本不起作用。

运行效果图

这其中的原因,笔者是这样认为的,首先,linearLayout,Button这些组件都是在android.widget这个包中的。他们是同一级别的。只是说linearLayout是一个ViewGroup可以再包含其他的View而已。不存在其他的优先级关系。

所以,笔者的理解是,如果Button3这个控件要同其他控件产生相互关系的话,首先他们是要位于同一级别的。(此处说的级别不是说组件级别,而是在xml文件里面设置的级别,如:linearLayout和button3是一级的话,那button2,textView,button3既是二级)
只有同一级别的才能设置关系,否则的话设置相互之间的位置关系就不会起作用。

这就是笔者的理解,根本不存在层的概念。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值