2011年4月29日 星期五

Androd layout properties

第一類:屬性值為true或false
android:layout_centerHrizontal                                           水平居中
android:layout_centerVertical                                            垂直居中
android:layout_centerInparent                                           相對於父元素完全居中
android:layout_alignParentBottom                                     貼緊父元素的下邊緣
android:layout_alignParentLeft                                          貼緊父元素的左邊緣
android:layout_alignParentRight                                        貼緊父元素的右邊緣
android:layout_alignParentTop                                          貼緊父元素的上邊緣
android:layout_alignWithParentIfMissing                            如果對應的兄弟元素找不到的話就以父元
素做參照物
第二類:屬性值必須為id的引用名「@id/id-name」
android:layout_below                          在某元素的下方
android:layout_above                          在某元素的的上方
android:layout_toLeftOf                       在某元素的左邊
android:layout_toRightOf                     在某元素的右邊
android:layout_alignTop                      本元素的上邊緣和某元素的的上邊緣對齊
android:layout_alignLeft                      本元素的左邊緣和某元素的的左邊緣對齊
android:layout_alignBottom                 本元素的下邊緣和某元素的的下邊緣對齊
android:layout_alignRight                    本元素的右邊緣和某元素的的右邊緣對齊
第三類:屬性值為具體的像素值,如30dip,40px
android:layout_marginBottom              離某元素底邊緣的距離
android:layout_marginLeft                   離某元素左邊緣的距離
android:layout_marginRight                 離某元素右邊緣的距離
android:layout_marginTop                   離某元素上邊緣的距離


Layout對於迅速的搭建界面和提高界面在不同分辨率的屏幕上的適應性具有很大的作用。這裡簡要介紹Android的Layout和研究一下它的實現。
Android有Layout:FrameLayout,LinearLayout,TableLayout,RelativeLayout,AbsoluteLayout。
放入Layout中進行排布的View的XML屬性:
1.幾種Layout中Item所共有的XML屬性:
(1)layout_width
(2)layout_height

(3)layout_marginLeft
(4)layout_marginTop
(5)layout_marginRight
(6)layout_marginBottom

(7)layout_gravity
FrameLayout是最簡單的Layout,就只具有這些屬性。
LinearLayout還會有:
(8 ) layout_weight
TableLayout的行TableRow是一個橫向的(horizontal)的LinearLayout。
RelativeLayout有16個align相關的XML屬性:
(9)layout_above
(10)layout_alignBaseline
(11)layout_alignBottom
(12)layout_alignLeft
(13)layout_alignParentBottom
(14)layout_alignParentLeft
(15)layout_alignParentRight
(16)layout_alignParentTop
(17)layout_alignRight
(18 ) layout_alignTop
(19)layout_below
(20)layout_centerHorizontal
(21)layout_centerInParent
(22)layout_centerVertical
(23)layout_toLeftOf
(24)layout_toRightOf
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
(1)和(2)用來確定放入Layout中的View的寬度和高度:它們的可能取值為fill_parent,wrap_content或者固定的像素值。
(3)(4)(5)(6)是放入Layout中的View期望它能夠和Layout的邊界或者其他View之間能夠相距一段距離。
(7)用來確定View在Layout中的停靠位置。
(8 ) 用於在LinearLayout中把所有子View排布之後的剩餘空間按照它們的layout_weight分配給各個擁有這個屬性的View。
(9)到(24)用來確定RelativeLayout中的View相對於Layout或者Layout中的其他View的位置。
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
根據Android的文檔,Android會對Layou和View嵌套組成的這棵樹進行2次遍歷,一次是measure調用,用來確定Layout或者View的大小;一次是layout調用,用來確定Layout或者view的位置。當然後來我自己的山寨實現把這2次調用合併到了一起。那就是Layout在排布之前都對自己進行measure一次,然後對View遞歸調用Layout方法。這樣子的大小肯定是確定了的。然後用確定了的大小來使用gravity或者align屬性來定位,使用margin來調整位置。

沒有留言:

張貼留言