新聞中心
Layout對(duì)于迅速的搭建界面和提高界面在不同分辨率的屏幕上的適應(yīng)性具有很大的作用。這里簡(jiǎn)要介紹Android的Layout和研究一下它的實(shí)現(xiàn)。

Android有Layout:FrameLayout,LinearLayout,TableLayout,RelativeLayout,AbsoluteLayout。
放入Layout中進(jìn)行排布的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是最簡(jiǎn)單的Layout,就只具有這些屬性。
LinearLayout還會(huì)有:
(8)layout_weight
TableLayout的行TableRow是一個(gè)橫向的(horizontal)的LinearLayout。
RelativeLayout有16個(gè)align相關(guān)的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)用來(lái)確定放入Layout中的View的寬度和高度:它們的可能取值為fill_parent,wrap_content或者固定的像素值。
(3)(4)(5)(6)是放入Layout中的View期望它能夠和Layout的邊界或者其他View之間能夠相距一段距離。
(7)用來(lái)確定View在Layout中的??课恢?。
(8)用于在LinearLayout中把所有子View排布之后的剩余空間按照它們的layout_weight分配給各個(gè)擁有這個(gè)屬性的View。
(9)到(24)用來(lái)確定RelativeLayout中的View相對(duì)于Layout或者Layout中的其他View的位置。
根據(jù)Android的文檔,Android會(huì)對(duì)Layou和View嵌套組成的這棵樹(shù)進(jìn)行2次遍歷,一次是measure調(diào)用,用來(lái)確定Layout或者View的大??;一次是layout調(diào)用,用來(lái)確定Layout或者view的位置。當(dāng)然后來(lái)我自己的山寨實(shí)現(xiàn)把這2次調(diào)用合并到了一起。那就是Layout在排布之前都對(duì)自己進(jìn)行measure一次,然后對(duì)View遞歸調(diào)用Layout方法。這樣子的大小肯定是確定了的。然后用確定了的大小來(lái)使用gravity或者align屬性來(lái)定位,使用margin來(lái)調(diào)整位置。
【編輯推薦】
- Android學(xué)習(xí)筆記:androidmanifest.xml解析
- 移動(dòng)開(kāi)發(fā)基礎(chǔ)教程:Android是什么
- Android SDK工具:使用layoutopt進(jìn)行布局優(yōu)化
- PlayBook運(yùn)行Android應(yīng)用的五大細(xì)節(jié)
- Kindle和Android開(kāi)發(fā)的比較
當(dāng)前文章:Android學(xué)習(xí)筆記:Layout.xml屬性
網(wǎng)頁(yè)URL:http://fisionsoft.com.cn/article/dpjpdjg.html


咨詢
建站咨詢
