论坛首页 移动开发技术论坛

关于RelativeLayout的一些看法,欢迎拍砖。

浏览 11295 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-02-27  

如需转载请著名

作者:倚风听雪         来源:http://hklongshao.iteye.com



首先,交代情况,以前很少用RelativeLayout,但是这次项目用到了,就拿来用,发现这东西太灵活了。很容易给人造成一些错误。

下面谈谈听雪的看法。

引用

From Tutorials:

If you find yourself using several nested LinearLayout groups, you may be able toreplace them with a single RelativeLayout


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

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

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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:background="#CCFFFF">
	<LinearLayout
		android:id="@+id/linearLayout"
		android:layout_width="fill_parent"
		android:layout_height="200dp"
		android:background="#32000033"
		android:orientation="vertical">
		<Button
			android:id="@+id/button1"
			android:layout_width="fill_parent"
			android:layout_height="wrap_content"
			android:background="#FF3300"
			android:text="Button" />
		<TextView
			android:id="@+id/textView"
			android:layout_width="fill_parent"
			android:layout_height="wrap_content"
			android:layout_weight="1"
			android:text="Base"
			android:textColor="#6633FF"
			android:gravity="center" />
		<Button
			android:id="@+id/button2"
			android:layout_width="fill_parent"
			android:layout_height="wrap_content"
			android:background="#FF3300"
			android:text="Button" />
	</LinearLayout>
	<Button
        android:id="@+id/button3"
		android:layout_width="100dp"
		android:layout_height="50dp"
		android:layout_centerInParent="true"
		android:layout_alignBottom="@id/linearLayout"
		android:text="button" />
</RelativeLayout>


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


运行效果图



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

android:layout_alignBottom="@id/button1"

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



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

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

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

欢迎各位拍砖。
  • 大小: 9.2 KB
  • 大小: 9.4 KB
   发表时间:2011-02-28  
View根据xml分层,嵌套  有钻研精神,佩服
0 请登录后投票
   发表时间:2011-03-02  
Relative就是给你一个基准值(上下左右中),然后其他组件根据这个基准值或者已有组件的相对位置来布局

至于好处嘛。。。。可以大量减少组件树的深度(太深会出stackoverflowException)
0 请登录后投票
   发表时间:2011-03-02  
也许这个有点帮助。
fyi: http://stackoverflow.com/questions/2990882/android-and-layouts/2991213#2991213
0 请登录后投票
   发表时间:2011-03-03  
nciky1984 写道
Relative就是给你一个基准值(上下左右中),然后其他组件根据这个基准值或者已有组件的相对位置来布局

至于好处嘛。。。。可以大量减少组件树的深度(太深会出stackoverflowException)



是的,而且如果Tree 的深度太深的话会在计算view显示大小的时候耗费过多资源,

如果多层layout嵌套的话,最好是直接用RelativeLayout布局来实现,这样可以减少对view大小的二次计算
0 请登录后投票
   发表时间:2011-03-05  
个人觉得,relative layout会造成代码的可读性很差,而且修改起来也很麻烦,你必须一个个组件查看位置,在脑海中构造一副图,才能正确拜访位置。而带来的性能提高却并不明显。在大多数情况,我宁愿用linear layout和table layout
0 请登录后投票
   发表时间:2011-04-22  
作用什么样的控件,要看当前的需求会是什么样的,LinearLayout 和RelativeLayout 的使用只要灵活的使用,效果都是一样的。
0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics