Android RelativeLayout 使用注意
Android界面布局中使用最多的可能是LinearLayout了。但是在某些復雜的布局情況中,如果使用不斷嵌套LinearLayout來進行布局,將會使整個布局文件十分的冗余,而且這樣冗余的布局界面。在Android系統生成布局樹的時候將會需要更多的內存資源,這顯然是不應該的。
A 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_CONTENTand a child set toALIGN_PARENT_BOTTOM. 在RelativeLayout中,不能有其子控件的位置和RelativeLayout大小之間的循環依賴;例如,不能設置一個RelativeLayout的高度為WRAP_CONTENT同時又設置他的一個子控件的位置為ALIGN_PARENT_BOTTOM. - 在程序中使用findViewById()來實例化RelativeLayout中的控件的時候。由于RelativeLayout中的View之間有關系。比如B在A的右邊且與A的頂部對齊,我們就得先實例化A才能實例化B。不然會得到一個Exception。(昨天就因為這個Exception找到凌晨1點... ...)
其它注意,暫時木有發現。
Finish Whatever U've Started !!!


浙公網安備 33010602011771號