[color=blue]如需转载请著名
作者:倚风听雪 来源:http://hklongshao.iteye.com [/color]
首先,交代情况,以前很少用RelativeLayout,但是这次项目用到了,就拿来用,发现这东西太灵活了。很容易给人造成一些错误。
下面谈谈听雪的看法。
[quote]
[color=red]From Tutorials:[/color]
If you find yourself using [color=blue]several nested LinearLayout groups[/color], you may be able toreplace them with a single [color=blue]RelativeLayout[/color][/quote]
以上来自Tutorials,听雪理解的观点是,当有过个ViewGroup嵌套的时候,再去考虑用RelativeLayout,听雪觉得既然官方这么写,很程度是因为,RelativeLayout太灵活了,它的灵活性给我们对UI的控制多少回造成一定影响。
曾经有人跟听雪说过,RelativeLayout跟FrameLayout有一些相似,给人的感觉是分层的。有层的这个概念。
听雪觉得不是这样的,是没有层的概念的。从官方的解释上可以看出这东西就是可以设置相对布局的一个布局而已。没有层的概念。
先上段代码,更直观的看看。
只贴xml,activity没什么东西,就显示一下罢了。
运行效果图
[align=center][img]http://dl.iteye.com/upload/attachment/426027/fa5a64a7-c748-3bfd-8406-5d8dbd25e29b.png[/img][/align]
很明显可以看出button3的下边缘是跟lineLayout的下边缘在一条水平线上的。
但是当像上面一样设置的时候,我们可能会是想让button3的下边缘跟button1的下边缘在一个水平线,但是这些写的效果却不是按我们所想的显示,如此设置根本不起作用。
[align=center][img]http://dl.iteye.com/upload/attachment/426032/9cc447cd-a051-3129-882e-66315464ced0.png[/img][/align]
[b]这其中的原因,听雪是这样认为的,首先,linearLayout,Button这些组件都是在[color=blue]android.widget[/color]这个包中的。他们是[color=blue]同一级别[/color]的。只是说linearLayout是一个ViewGroup可以再包含其他的View而已。不存在其他的优先级关系。
所以,听雪的理解是,如果Button3这个控件要同其他控件产生相互关系的话,首先他们是要位于同一级别的。([color=red]此处说的级别不是说组件级别,而是在xml文件里面设置的级别,如:linearLayout和button3是一级的话,那button2,textView,button3既是二级[/color])
只有同一级别的才能设置关系,否则的话设置相互之间的位置关系就不会起作用。
这就是听雪的理解,根本不存在层的概念。
欢迎各位拍砖。[/b]
作者:倚风听雪 来源:http://hklongshao.iteye.com [/color]
首先,交代情况,以前很少用RelativeLayout,但是这次项目用到了,就拿来用,发现这东西太灵活了。很容易给人造成一些错误。
下面谈谈听雪的看法。
[quote]
[color=red]From Tutorials:[/color]
If you find yourself using [color=blue]several nested LinearLayout groups[/color], you may be able toreplace them with a single [color=blue]RelativeLayout[/color][/quote]
以上来自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没什么东西,就显示一下罢了。
运行效果图
[align=center][img]http://dl.iteye.com/upload/attachment/426027/fa5a64a7-c748-3bfd-8406-5d8dbd25e29b.png[/img][/align]
很明显可以看出button3的下边缘是跟lineLayout的下边缘在一条水平线上的。
android:layout_alignBottom="@id/button1"
但是当像上面一样设置的时候,我们可能会是想让button3的下边缘跟button1的下边缘在一个水平线,但是这些写的效果却不是按我们所想的显示,如此设置根本不起作用。
[align=center][img]http://dl.iteye.com/upload/attachment/426032/9cc447cd-a051-3129-882e-66315464ced0.png[/img][/align]
[b]这其中的原因,听雪是这样认为的,首先,linearLayout,Button这些组件都是在[color=blue]android.widget[/color]这个包中的。他们是[color=blue]同一级别[/color]的。只是说linearLayout是一个ViewGroup可以再包含其他的View而已。不存在其他的优先级关系。
所以,听雪的理解是,如果Button3这个控件要同其他控件产生相互关系的话,首先他们是要位于同一级别的。([color=red]此处说的级别不是说组件级别,而是在xml文件里面设置的级别,如:linearLayout和button3是一级的话,那button2,textView,button3既是二级[/color])
只有同一级别的才能设置关系,否则的话设置相互之间的位置关系就不会起作用。
这就是听雪的理解,根本不存在层的概念。
欢迎各位拍砖。[/b]