Android RelativeLayout 使用注意

  Android界面布局中使用最多的可能是LinearLayout了。但是在某些复杂的布局情况中,如果使用不断嵌套LinearLayout来进行布局,将会使整个布局文件十分的冗余,而且这样冗余的布局界面。在Android系统生成布局树的时候将会需要更多的内存资源,这显然是不应该的。

    RelativeLayout is a very powerful utility for designing a user interface because it can eliminate nested ViewGroups. If you find yourself using several nestedLinearLayout groups, you may be able to replace them with a single RelativeLayout.

  RelativeLayout是一个十分强大的设计UI布局的Layout,因为他可以消除嵌套的ViewGroups。如果你正在使用几个嵌套的LinearLayout来实现你的布局,你应该使用一个RelativeLayout来代替这些LinearLayout。

  因此,在某些简单的布局中我们可以使用LinearLayout来进行布局。更复杂的情况下,就要考虑其它布局了。

  下面介绍一下RelativeLayout使用时的注意事项,这些都是我第一次使用时遇到的问题,且花了很多时间来解决。写在这里希望对需要的同学有帮助。

    • Note that you cannot have a circular dependency between the size of the RelativeLayout and the position of its children. For example, you cannot have a RelativeLayout whose height is set to WRAP_CONTENT and a child set to ALIGN_PARENT_BOTTOM.  在RelativeLayout中,不能有其子控件的位置和RelativeLayout大小之间的循环依赖;例如,不能设置一个RelativeLayout的高度为WRAP_CONTENT同时又设置他的一个子控件的位置为ALIGN_PARENT_BOTTOM.
    • 在程序中使用findViewById()来实例化RelativeLayout中的控件的时候。由于RelativeLayout中的View之间有关系。比如B在A的右边且与A的顶部对齐,我们就得先实例化A才能实例化B。不然会得到一个Exception。(昨天就因为这个Exception找到凌晨1点... ...)
  其它注意,暂时木有发现。
posted @ 2011-07-26 11:28  Cat_Lee  阅读(1911)  评论(1编辑  收藏  举报