Android學習筆記:Layout.xml屬性

來源:互聯網
上載者:User

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來調整位置。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.