如需转载请著名
作者:倚风听雪 来源: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
分享到:
相关推荐
Android 开发入门 对 RelativeLayout的基本属性的讲解和用法 ,更容易的帮你理解和使用
自定义RelativeLayout圆角图片背景,自定义属性 背景 圆角半径
介绍一下RelativeLayout的一些属性,针对于刚学习android开发的朋友
Android 相对布局 RelativeLayout 属性
Android RelativeLayout 的翻译应用。希望能给大家帮助
Android布局入门,LinearLayout和RelativeLayout的理解和应用
RelativeLayout 上 下 中 左 右布局 demo
总结android中相对布局RelativeLayout的所有属性及使用方法。适合初级程序员
android布局属性RelativeLayout详解.
介绍了android 中 RelativeLayout 下的各种属性。linux下打开.
LinearLayout与RelativeLayout混合用
Android相对布局RelativeLayout的基本用法,相对布局是最实用的布局。
相对布局我最新喜欢的布局,也是game开发,照相机以及增钱现实中最常用的布局!
RelativeLayout android 布局练习
安卓动态控制RelativeLayout的组件
android实现自定义RelativeLayout可拖动、缩放、旋转TextView
RelativeLayout点击效果,背景色的变化,通过两种方法:1. 直接用图片资源; 2. 用color资源
像 RelativeLayout 一样的 RadioGroup
LinearLayout和RelativeLayout实现精确布局-简单版 类似与应用评价
DragView 可拖拽的view,继承自RelativeLayout