Android開發(fā)學(xué)習(xí)筆記:5大布局方式詳解

Android中常用的5大布局方式有以下幾種:

線性布局(LinearLayout):按照垂直或者水平方向布局的組件虽风。

幀布局(FrameLayout):組件從屏幕左上方布局組件温鸽。

表格布局(TableLayout):按照行列方式布局組件。

相對(duì)布局(RelativeLayout):相對(duì)其它組件的布局方式秉版。

絕對(duì)布局(AbsoluteLayout):按照絕對(duì)坐標(biāo)來布局組件贤重。

1. 線性布局

線性布局是Android開發(fā)中最常見的一種布局方式,它是按照垂直或者水平方向來布局清焕,通過“android:orientation”屬性可以設(shè)置線性布局的方向并蝗。屬性值有垂直(vertical)和水平(horizontal)兩種。

常用的屬性:

android:orientation:可以設(shè)置布局的方向

android:gravity:用來控制組件的對(duì)齊方式

layout_weight:控制各個(gè)組件在布局中的相對(duì)大小

第一個(gè)實(shí)例

①效果圖:

②核心代碼如下:

main.xml

<?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"

? ? >? ? ?

? ? <LinearLayout

? ? ? ? android:layout_width="fill_parent"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:orientation="vertical"

? ? ? ? >

? ? ? ? <EditText

? ? ? ? ? ? android:layout_width="fill_parent"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? />

? ? </LinearLayout>

? ? <LinearLayout

? ? ? ? android:layout_width="fill_parent"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:orientation="horizontal"

? ? ? ? android:gravity="right"

? ? ? ? >

? ? <!-- android:gravity="right"表示Button組件向右對(duì)齊 -->

? ? ? ? <Button

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:layout_width="wrap_content"

? ? ? ? ? ? android:text="確定"

? ? ? ? ? ? />

? ? ? ? <Button

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:layout_width="wrap_content"

? ? ? ? ? ? android:text="取消"

? ? ? ? ? ? />? ?

? ? </LinearLayout>

</LinearLayout>

第二個(gè)實(shí)例

①效果圖:

②核心代碼:

mian.xml

<?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">

? ? <LinearLayout

? ? android:orientation="horizontal"

? ? android:layout_width="fill_parent"

? ? android:layout_height="fill_parent"

? ? android:layout_weight="1">


? ? <TextView

? ? ? ? android:text="red"

? ? ? ? android:gravity="center_horizontal"

? ? ? ? android:background="#aa0000"

? ? ? ? android:layout_width="wrap_content"

? ? ? ? android:layout_height="fill_parent"

? ? ? ? android:layout_weight="1"

? ? ? ? />

? ? <!--android:gravity="center_horizontal"水平居中 -->?

? ? <!--layout_weight屬性以控制各個(gè)控件在布局中的相對(duì)大小耐朴。layout_weight屬性是一個(gè)非負(fù)整數(shù)值借卧。?

? ? ? ? 線性布局會(huì)根據(jù)該控件layout_weight值與其所處布局中所有控件layout_weight值之和的比值為該控件分配占用的區(qū)域盹憎。?

? ? ? ? 例如筛峭,在水平布局的LinearLayout中有兩個(gè)Button,這兩個(gè)Button的layout_weight屬性值都為1陪每,?

? ? ? ? 那么這兩個(gè)按鈕都會(huì)被拉伸到整個(gè)屏幕寬度的一半影晓。如果layout_weight指為0,控件會(huì)按原大小顯示檩禾,不會(huì)被拉伸挂签;?

? ? ? ? 對(duì)于其余l(xiāng)ayout_weight屬性值大于0的控件,系統(tǒng)將會(huì)減去layout_weight屬性值為0的控件的寬度或者高度盼产,?

? ? ? ? 再用剩余的寬度或高度按相應(yīng)的比例來分配每一個(gè)控件顯示的寬度或高度-->

? ? <TextView

? ? ? ? android:text="Teal"

? ? ? ? android:gravity="center_horizontal"

? ? ? ? android:background="#008080"

? ? ? ? android:layout_width="wrap_content"

? ? ? ? android:layout_height="fill_parent"

? ? ? ? android:layout_weight="1"/>


? ? <TextView

? ? ? ? android:text="blue"

? ? ? ? android:gravity="center_horizontal"

? ? ? ? android:background="#0000aa"

? ? ? ? android:layout_width="wrap_content"

? ? ? ? android:layout_height="fill_parent"

? ? ? ? android:layout_weight="1"

? ? ? ? />


? ? <TextView

? ? ? ? android:text="orange"

? ? ? ? android:gravity="center_horizontal"

? ? ? ? android:background="#FFA500"

? ? ? ? android:layout_width="wrap_content"

? ? ? ? android:layout_height="fill_parent"

? ? ? ? android:layout_weight="1"

? ? ? ? />


? ? </LinearLayout>?

? ? <LinearLayout

? ? android:orientation="vertical"

? ? android:layout_width="fill_parent"

? ? android:layout_height="fill_parent"

? ? android:layout_weight="1">


? ? <TextView

? ? ? ? android:text="row one"

? ? ? ? android:textSize="15pt"

? ? ? ? android:background="#aa0000"

? ? ? ? android:layout_width="fill_parent"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:layout_weight="1"

? ? ? ? />

? ? <!--? -->?

? ? <TextView

? ? ? ? android:text="row two"

? ? ? ? android:textSize="15pt"

? ? ? ? android:background="#DDA0DD"

? ? ? ? android:layout_width="fill_parent"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:layout_weight="1"

? ? ? ? />


? ? <TextView

? ? ? ? android:text="row three"

? ? ? ? android:textSize="15pt"

? ? ? ? android:background="#008080"

? ? ? ? android:layout_width="fill_parent"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:layout_weight="1"

? ? ? ? />? ?

? ? <TextView

? ? ? ? android:text="row four"

? ? ? ? android:textSize="15pt"

? ? ? ? android:background="#FFA500"

? ? ? ? android:layout_width="fill_parent"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:layout_weight="1"

? ? ? ? />? ? ?

? ? </LinearLayout>?

</LinearLayout>

2. 幀布局

幀布局是從屏幕的左上角(0,0)坐標(biāo)開始布局饵婆,多個(gè)組件層疊排列,第一個(gè)添加的組件放到最底層戏售,最后添加到框架中的視圖顯示在最上面侨核。上一層的會(huì)覆蓋下一層的控件草穆。

簡(jiǎn)單的例子

①效果圖:

② 核心代碼:

main.xml

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

? ? android:layout_width="fill_parent"

? ? android:layout_height="fill_parent"

? ? >

? ? <TextView? ?

? ? ? ? android:layout_width="300dp"?

? ? ? ? android:layout_height="300dp"?

? ? ? ? android:background="#00BFFF"? ? ? ? ?

? ? ? ? />

? ? <TextView? ?

? ? ? ? android:layout_width="260dp"?

? ? ? ? android:layout_height="260dp"?

? ? ? ? android:background="#FFC0CB"? ? ? ? ?

? ? ? ? />

? ? <TextView? ?

? ? ? ? android:layout_width="220dp"?

? ? ? ? android:layout_height="220dp"?

? ? ? ? android:background="#0000FF"? ? ? ? ?

? ? ? ? />

</FrameLayout>

3.表格布局

表格布局是一個(gè)ViewGroup以表格顯示它的子視圖(view)元素,即行和列標(biāo)識(shí)一個(gè)視圖的位置搓译。

表格布局常用的屬性如下:

android:collapseColumns:隱藏指定的列

android:shrinkColumns:收縮指定的列以適合屏幕悲柱,不會(huì)擠出屏幕

android:stretchColumns:盡量把指定的列填充空白部分

android:layout_column:控件放在指定的列

android:layout_span:該控件所跨越的列數(shù)

簡(jiǎn)單的列子:

①效果圖:

② 核心代碼:

main.xml

<?xml version="1.0" encoding="utf-8"?>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"

? ? android:layout_width="fill_parent"

? ? android:layout_height="fill_parent"

? ? >

? ? <TableRow>

? ? ? ? <Button

? ? ? ? ? ? android:text="Button1"

? ? ? ? ? ? />

? ? ? ? <Button

? ? ? ? ? ? android:text="Button2"

? ? ? ? ? ? />

? ? ? ? <Button

? ? ? ? ? ? android:text="Button3"

? ? ? ? ? ? />

? ? </TableRow>

? ? <TableRow>

? ? ? ? <Button

? ? ? ? ? ? android:text="Button4"

? ? ? ? ? ? />

? ? ? ? <Button

? ? ? ? ? ? android:layout_span="2"

? ? ? ? ? ? android:text="Button5"

? ? ? ? ? ? />

? ? </TableRow>


</TableLayout>

4.相對(duì)布局

相對(duì)布局是按照組件之間的相對(duì)位置來布局,比如在某個(gè)組件的左邊些己,右邊豌鸡,上面和下面等。

相對(duì)布局常用屬性請(qǐng)參考我博客的:http://liangruijun.blog.51cto.com/3061169/631816

簡(jiǎn)單的例子

①效果圖:

② 核心代碼:

main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

? ? android:layout_width="fill_parent"

? ? android:layout_height="wrap_content"

? ? android:padding="10px"

? ? >

? ? <TextView? ?

? ? ? ? android:id="@+id/tev1"

? ? ? ? android:layout_width="wrap_content"?

? ? ? ? android:layout_height="wrap_content"?

? ? ? ? android:layout_marginBottom="30dp"

? ? ? ? android:text="Please Type Here:"

? ? ? ? />

? ? <EditText

? ? ? ? android:id="@+id/tx1"

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:layout_below="@id/tev1"

? ? ? ? />

? ? <Button

? ? ? ? android:id="@+id/btn1"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:layout_width="wrap_content"

? ? ? ? android:layout_below="@id/tx1"

? ? ? ? android:layout_alignParentRight="true"

? ? ? ? android:text="確定"

? ? ? ? />

? ? <Button

? ? ? ? android:id="@+id/btn2"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:layout_width="wrap_content"

? ? ? ? android:layout_below="@id/tx1"

? ? ? ? android:layout_toLeftOf="@id/btn1"

? ? ? ? android:layout_marginRight="30dp"

? ? ? ? android:text="取消"

? ? ? ? />

</RelativeLayout>

東莞網(wǎng)站建設(shè)www.zg886.cn

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末段标,一起剝皮案震驚了整個(gè)濱河市涯冠,隨后出現(xiàn)的幾起案子跷车,更是在濱河造成了極大的恐慌塌西,老刑警劉巖俊啼,帶你破解...
    沈念sama閱讀 222,464評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件嘴脾,死亡現(xiàn)場(chǎng)離奇詭異拦盹,居然都是意外死亡瓷产,警方通過查閱死者的電腦和手機(jī)搜贤,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,033評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門版仔,熙熙樓的掌柜王于貴愁眉苦臉地迎上來虑灰,“玉大人吨瞎,你說我怎么就攤上這事∧赂溃” “怎么了颤诀?”我有些...
    開封第一講書人閱讀 169,078評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)对湃。 經(jīng)常有香客問我崖叫,道長(zhǎng),這世上最難降的妖魔是什么拍柒? 我笑而不...
    開封第一講書人閱讀 59,979評(píng)論 1 299
  • 正文 為了忘掉前任心傀,我火速辦了婚禮,結(jié)果婚禮上拆讯,老公的妹妹穿的比我還像新娘脂男。我一直安慰自己,他們只是感情好种呐,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,001評(píng)論 6 398
  • 文/花漫 我一把揭開白布宰翅。 她就那樣靜靜地躺著,像睡著了一般爽室。 火紅的嫁衣襯著肌膚如雪汁讼。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,584評(píng)論 1 312
  • 那天,我揣著相機(jī)與錄音嘿架,去河邊找鬼卜录。 笑死,一個(gè)胖子當(dāng)著我的面吹牛眶明,可吹牛的內(nèi)容都是我干的艰毒。 我是一名探鬼主播,決...
    沈念sama閱讀 41,085評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼搜囱,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼丑瞧!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起蜀肘,我...
    開封第一講書人閱讀 40,023評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤绊汹,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后扮宠,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體西乖,經(jīng)...
    沈念sama閱讀 46,555評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,626評(píng)論 3 342
  • 正文 我和宋清朗相戀三年坛增,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了获雕。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,769評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡收捣,死狀恐怖届案,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情罢艾,我是刑警寧澤楣颠,帶...
    沈念sama閱讀 36,439評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站咐蚯,受9級(jí)特大地震影響童漩,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜春锋,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,115評(píng)論 3 335
  • 文/蒙蒙 一矫膨、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧看疙,春花似錦豆拨、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,601評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽脚线。三九已至搁胆,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背渠旁。 一陣腳步聲響...
    開封第一講書人閱讀 33,702評(píng)論 1 274
  • 我被黑心中介騙來泰國打工攀例, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人顾腊。 一個(gè)月前我還...
    沈念sama閱讀 49,191評(píng)論 3 378
  • 正文 我出身青樓粤铭,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國和親杂靶。 傳聞我的和親對(duì)象是個(gè)殘疾皇子梆惯,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,781評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容