Android 五大布局(LinearLayout宦棺、FrameLayout、AbsoulteLayout黔帕、RelativeLayout代咸、TableLayout )

http://lib.csdn.net/article/android/2581?knId=297

Android的界面是有布局和組件協(xié)同完成的,布局好比是建筑里的框架成黄,而組件則相當于建筑里的磚瓦呐芥。組件按照布局的要求依次排列,就組成了用戶所看見的界面奋岁。Android的五大布局分別是LinearLayout(線性布局)思瘟、FrameLayout(單幀布局)、RelativeLayout(相對布局)厦取、AbsoluteLayout(絕對布局)和TableLayout(表格布局)潮太。

LinearLayout:
  LinearLayout按照垂直或者水平的順序依次排列子元素管搪,每一個子元素都位于前一個元素之后虾攻。如果是垂直排列,那么將是一個N行單列的結構更鲁,每一行只會有一個元素霎箍,而不論這個元素的寬度為多少;如果是水平排列澡为,那么將是一個單行N列的結構漂坏。如果搭建兩行兩列的結構,通常的方式是先垂直排列兩個元素媒至,每一個元素里再包含一個LinearLayout進行水平排列顶别。
  LinearLayout中的子元素屬性android:layout_weight生效,它用于描述該子元素在剩余空間中占有的大小比例拒啰。加入一行只有一個文本框驯绎,那么它的默認值就為0,如果一行中有兩個等長的文本框谋旦,那么他們的android:layout_weight值可以是同為1剩失。如果一行中有兩個不等長的文本框,那么他們的android:layout_weight值分別為1和2册着,那么第一個文本框將占據(jù)剩余空間的三分之二拴孤,第二個文本框將占據(jù)剩余空間中的三分之一。android:layout_weight遵循數(shù)值越小甲捏,重要度越高的原則演熟。顯示效果如下:


LinearLayout <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ff000000" android:text="@string/hello"/> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ff654321" android:layout_weight="1" android:text="1"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#fffedcba" android:layout_weight="2" android:text="2"/> </LinearLayout></LinearLayout>
 FrameLayout:
  FrameLayout是五大布局中最簡單的一個布局,在這個布局中司顿,整個界面被當成一塊空白備用區(qū)域芒粹,所有的子元素都不能被指定放置的位置蚕冬,它們統(tǒng)統(tǒng)放于這塊區(qū)域的左上角,并且后面的子元素直接覆蓋在前面的子元素之上是辕,將前面的子元素部分和全部遮擋囤热。顯示效果如下,第一個TextView被第二個TextView完全遮擋获三,第三個TextView遮擋了第二個TextView的部分位置旁蔼。


FrameLayout <?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ff000000" android:gravity="center" android:text="1"/> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ff654321" android:gravity="center" android:text="2"/> <TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#fffedcba" android:gravity="center" android:text="3"/></FrameLayout>
 AbsoluteLayout:
  AbsoluteLayout是絕對位置布局。在此布局中的子元素的android:layout_x和android:layout_y屬性將生效疙教,用于描述該子元素的坐標位置棺聊。屏幕左上角為坐標原點(0,0),第一個0代表橫坐標贞谓,向右移動此值增大限佩,第二個0代表縱坐標,向下移動裸弦,此值增大祟同。在此布局中的子元素可以相互重疊。在實際開發(fā)中理疙,通常不采用此布局格式晕城,因為它的界面代碼過于剛性,以至于有可能不能很好的適配各種終端窖贤。顯示效果如下:

AbsoluteLayout <?xml version="1.0" encoding="utf-8"?><AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#ffffffff" android:gravity="center" android:layout_x="50dp" android:layout_y="50dp" android:text="1"/> <TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#ff654321" android:gravity="center" android:layout_x="25dp" android:layout_y="25dp" android:text="2"/> <TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#fffedcba" android:gravity="center" android:layout_x="125dp" android:layout_y="125dp" android:text="3"/></AbsoluteLayout>
RelativeLayout:
  RelativeLayout按照各子元素之間的位置關系完成布局砖顷。在此布局中的子元素里與位置相關的屬性將生效。例如android:layout_below, android:layout_above等赃梧。子元素就通過這些屬性和各自的ID配合指定位置關系滤蝠。注意在指定位置關系時,引用的ID必須在引用之前授嘀,先被定義物咳,否則將出現(xiàn)異常。
  RelativeLayout里常用的位置屬性如下:    android:layout_toLeftOf —— 該組件位于引用組件的左方    android:layout_toRightOf —— 該組件位于引用組件的右方    android:layout_above —— 該組件位于引用組件的上方    android:layout_below —— 該組件位于引用組件的下方    android:layout_alignParentLeft —— 該組件是否對齊父組件的左端    android:layout_alignParentRight —— 該組件是否齊其父組件的右端    android:layout_alignParentTop —— 該組件是否對齊父組件的頂部    android:layout_alignParentBottom —— 該組件是否對齊父組件的底部    android:layout_centerInParent —— 該組件是否相對于父組件居中    android:layout_centerHorizontal —— 該組件是否橫向居中    android:layout_centerVertical —— 該組件是否垂直居中
  RelativeLayout是Android五大布局結構中最靈活的一種布局結構粤攒,比較適合一些復雜界面的布局所森。下面示例就展示這么一個情況,第一個文本框與父組件的底部對齊夯接,第二個文本框位于第一個文本框的上方焕济,并且第三個文本框位于第二個文本框的左方。

RelativeLayout <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/text_01" android:layout_width="50dp" android:layout_height="50dp" android:background="#ffffffff" android:gravity="center" android:layout_alignParentBottom="true" android:text="1"/> <TextView android:id="@+id/text_02" android:layout_width="50dp" android:layout_height="50dp" android:background="#ff654321" android:gravity="center" android:layout_above="@id/text_01" android:layout_centerHorizontal="true" android:text="2"/> <TextView android:id="@+id/text_03" android:layout_width="50dp" android:layout_height="50dp" android:background="#fffedcba" android:gravity="center" android:layout_toLeftOf="@id/text_02" android:layout_above="@id/text_01" android:text="3"/></RelativeLayout>
TableLayout:
  TableLayout顧名思義盔几,此布局為表格布局晴弃,適用于N行N列的布局格式。一個TableLayout由許多TableRow組成,一個TableRow就代表TableLayout中的一行上鞠。
  TableRow是LinearLayout的子類际邻,它的android:orientation屬性值恒為horizontal,并且它的android:layout_width和android:layout_height屬性值恒為MATCH_PARENT和WRAP_CONTENT芍阎。所以它的子元素都是橫向排列世曾,并且寬高一致的。這樣的設計使得每個TableRow里的子元素都相當于表格中的單元格一樣谴咸。在TableRow中轮听,單元格可以為空,但是不能跨列岭佳。
  下面示例演示了一個TableLayout的布局結構血巍,其中第二行只有兩個單元格,而其余行都是三個單元格珊随。

TableLayout <?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:background="#ffffffff" android:gravity="center" android:padding="10dp" android:text="1"/> <TextView android:background="#ff654321" android:gravity="center" android:padding="10dp" android:text="2"/> <TextView android:background="#fffedcba" android:gravity="center" android:padding="10dp" android:text="3"/> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:background="#ff654321" android:gravity="center" android:padding="10dp" android:text="2"/> <TextView android:background="#fffedcba" android:gravity="center" android:padding="10dp" android:text="3"/> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:background="#fffedcba" android:gravity="center" android:padding="10dp" android:text="3"/> <TextView android:background="#ff654321" android:gravity="center" android:padding="10dp" android:text="2"/> <TextView android:background="#ffffffff" android:gravity="center" android:padding="10dp" android:text="1"/> </TableRow></TableLayout>

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末述寡,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子叶洞,更是在濱河造成了極大的恐慌鲫凶,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,682評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件京办,死亡現(xiàn)場離奇詭異掀序,居然都是意外死亡,警方通過查閱死者的電腦和手機惭婿,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來叶雹,“玉大人财饥,你說我怎么就攤上這事≌刍蓿” “怎么了钥星?”我有些...
    開封第一講書人閱讀 165,083評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長满着。 經常有香客問我谦炒,道長,這世上最難降的妖魔是什么风喇? 我笑而不...
    開封第一講書人閱讀 58,763評論 1 295
  • 正文 為了忘掉前任宁改,我火速辦了婚禮,結果婚禮上魂莫,老公的妹妹穿的比我還像新娘还蹲。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 67,785評論 6 392
  • 文/花漫 我一把揭開白布谜喊。 她就那樣靜靜地躺著潭兽,像睡著了一般。 火紅的嫁衣襯著肌膚如雪斗遏。 梳的紋絲不亂的頭發(fā)上山卦,一...
    開封第一講書人閱讀 51,624評論 1 305
  • 那天,我揣著相機與錄音诵次,去河邊找鬼怒坯。 笑死,一個胖子當著我的面吹牛藻懒,可吹牛的內容都是我干的剔猿。 我是一名探鬼主播,決...
    沈念sama閱讀 40,358評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼嬉荆,長吁一口氣:“原來是場噩夢啊……” “哼归敬!你這毒婦竟也來了?” 一聲冷哼從身側響起鄙早,我...
    開封第一講書人閱讀 39,261評論 0 276
  • 序言:老撾萬榮一對情侶失蹤汪茧,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后限番,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體舱污,經...
    沈念sama閱讀 45,722評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年弥虐,在試婚紗的時候發(fā)現(xiàn)自己被綠了扩灯。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,030評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡霜瘪,死狀恐怖珠插,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情颖对,我是刑警寧澤捻撑,帶...
    沈念sama閱讀 35,737評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站缤底,受9級特大地震影響顾患,放射性物質發(fā)生泄漏。R本人自食惡果不足惜个唧,卻給世界環(huán)境...
    茶點故事閱讀 41,360評論 3 330
  • 文/蒙蒙 一江解、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧坑鱼,春花似錦膘流、人聲如沸絮缅。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,941評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽耕魄。三九已至,卻和暖如春彭谁,著一層夾襖步出監(jiān)牢的瞬間吸奴,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,057評論 1 270
  • 我被黑心中介騙來泰國打工缠局, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留则奥,地道東北人。 一個月前我還...
    沈念sama閱讀 48,237評論 3 371
  • 正文 我出身青樓狭园,卻偏偏與公主長得像读处,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子唱矛,可洞房花燭夜當晚...
    茶點故事閱讀 44,976評論 2 355

推薦閱讀更多精彩內容