LinearLayout線性布局詳解

線性布局是Android UI頁面開發(fā)常用的布局比规,LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. 根據(jù)官網(wǎng)的描述可以解釋為:LinearLayout是一個視圖組,它將所有子項在一個方向上灾常,垂直或水平對齊。

編寫xml布局

Android中的頁面布局使用xml文檔描述雕什,xml是一種類似HTML的標(biāo)簽語言壹士,可以用來標(biāo)記數(shù)據(jù)倦春、定義數(shù)據(jù)類型,可以允許用戶對自己標(biāo)記語言進(jìn)行定義。我們打開Android Studio創(chuàng)建一個Android項目抄瑟,默認(rèn)會創(chuàng)建一個MainActivity頁面皮假,同時生成一個布局文件activity_main.xml,文件的路徑如下圖所示。

Android Studio界面

默認(rèn)創(chuàng)建的activity_main.xml使用的主布局是ConstraintLayout約束布局,我們可以把約束布局刪除乖杠,也可以在約束布局中嵌套線性布局畏吓。如果刪除約束布局庵佣,請保留xml文件的第一行。下面我們嘗試編寫一下xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">

</LinearLayout>

編輯xml布局文件需要帶上android命名空間xmlns:android="http://schemas.android.com/apk/res/android"肛根,否則會因無法識別而報錯臼氨。layout_widthlayout_height為必須屬性,分別表示布局的寬和高持隧,屬性名前面帶上android前綴。orientation屬性表示子項排列方向呀狼,也建議帶上該屬性,默認(rèn)值為horizontal橫向排列她奥。

LinearLayout線性布局

上圖右邊為LinearLayout線性布局效果拳恋,啥也沒有隙赁,因為我們啥也沒寫...

線性布局方向和尺寸

上面簡單提及了一下線性布局的layout_width, layout_height, orientation屬性,分別表示布局的寬弟灼、高和排列方向田绑。寬和高屬性的值可以為match_parent, wrap_content, 和固定數(shù)值尺寸。Android中推薦的長度尺寸單位是 dp,它表示像素密度缴挖,與像素單位 px的區(qū)別是可以根據(jù)分辨率調(diào)整展示苟鸯,減少跨設(shè)備顯示差異早处。

orientation屬性有兩個值:horizontal和vertical咸包,分別表示橫向和縱向排列子項。下面為LinearLayout添加兩個TextView作為子項,展示horizontal和vertical的差異葛账。

Vertical:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">
 <TextView
 android:text="this is TextView1"
 android:textColor="@color/black"
 android:textSize="18sp"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"/>
 <TextView
 android:text="this is TextView1"
 android:textColor="@color/black"
 android:textSize="18sp"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"/>

</LinearLayout>
vertical

Horizontal:代碼部分僅僅把vertical改為了horizontal

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="horizontal">
 <TextView
 android:text="this is TextView1"
 android:textColor="@color/black"
 android:textSize="18sp"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"/>
 <TextView
 android:text="this is TextView1"
 android:textColor="@color/black"
 android:textSize="18sp"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"/>

</LinearLayout>
horizontal

線性布局子項權(quán)重

子項可以通過設(shè)置layout_weight屬性來設(shè)置自身寬高占容器布局的比例宣谈,如果每個子項要平均分配漩怎,將layout_weight屬性值設(shè)置為1即可叁执,同時需要將layout_width寬度設(shè)置為0dp(對于橫向排列來說)吆录。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="horizontal">
 <TextView
 android:text="this is TextView1"
 android:textColor="@color/black"
 android:textSize="18sp"
 android:layout_weight="2"
 android:background="@color/teal_200"
 android:layout_width="0dp"
 android:layout_height="wrap_content"/>
 <TextView
 android:text="this is TextView2"
 android:textColor="@color/black"
 android:textSize="18sp"
 android:layout_weight="1"
 android:background="@color/purple_200"
 android:layout_width="0dp"
 android:layout_height="wrap_content"/>

</LinearLayout>
layout_weight

線性布局的嵌套

線性布局可以嵌套線性布局抱究,也可以嵌套其它布局。下面示例使用線性布局加權(quán)重實現(xiàn)底部帶圖片的導(dǎo)航欄樣式:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:gravity="bottom"
 android:orientation="horizontal">
 <LinearLayout
 android:layout_weight="1"
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:orientation="vertical">
 <ImageView
 android:src="@mipmap/ic_launcher"
 android:layout_width="40dp"
 android:layout_height="40dp"
 android:layout_gravity="center"/>
 <TextView
 android:text="第一項"
 android:textColor="@color/black"
 android:textSize="18sp"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center"/>
 </LinearLayout>
 <LinearLayout
 android:layout_weight="1"
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:orientation="vertical">
 <ImageView
 android:src="@mipmap/ic_launcher"
 android:layout_width="40dp"
 android:layout_height="40dp"
 android:layout_gravity="center"/>
 <TextView
 android:text="第二項"
 android:textColor="@color/black"
 android:textSize="18sp"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center"/>
 </LinearLayout>
 <LinearLayout
 android:layout_weight="1"
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:orientation="vertical">
 <ImageView
 android:src="@mipmap/ic_launcher"
 android:layout_width="40dp"
 android:layout_height="40dp"
 android:layout_gravity="center"/>
 <TextView
 android:text="第三項"
 android:textColor="@color/black"
 android:textSize="18sp"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center"/>
 </LinearLayout>
 <LinearLayout
 android:layout_weight="1"
 android:layout_width="0dp"
 android:layout_height="wrap_content"
 android:orientation="vertical">
 <ImageView
 android:src="@mipmap/ic_launcher"
 android:layout_width="40dp"
 android:layout_height="40dp"
 android:layout_gravity="center"/>
 <TextView
 android:text="第四項"
 android:textColor="@color/black"
 android:textSize="18sp"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center"/>
 </LinearLayout>

</LinearLayout>

效果預(yù)覽:

布局嵌套

上面的示例看著代碼比較多,其實每個子項都是一個LinearLayout包裹著一個圖片框和一個文本框,重復(fù)渲染了四遍,每個子項LinearLayout平均分配外層布局的寬度。

其它常用屬性

除了以上的layout_width, layout_height, orientation, layout_weight 屬性,還經(jīng)常用到下列屬性:

屬性 描述
android:id 為控件指定相應(yīng)的ID
android:padding 指定控件的內(nèi)邊距,控件當(dāng)中的內(nèi)容
android:layout_margin 視圖與周圍視圖的距離(外邊距)
android:minWidth 視圖最小寬度
android:minHeight 視圖最小高度
android:background 視圖的背景: 可以是顏色氯窍,也可以是圖片
android:visible 視圖的可視屬性
android:layout_gravity 視圖與上級視圖的對齊方式
android:gravity 視圖中內(nèi)容與視圖的對齊方式
android:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末朽基,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子霎俩,更是在濱河造成了極大的恐慌沉眶,老刑警劉巖传藏,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件彤守,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)起宽,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進(jìn)店門腐晾,熙熙樓的掌柜王于貴愁眉苦臉地迎上來藻糖,“玉大人,你說我怎么就攤上這事瘪弓×缆牵” “怎么了疹味?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長帜篇。 經(jīng)常有香客問我糙捺,道長,這世上最難降的妖魔是什么笙隙? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任洪灯,我火速辦了婚禮,結(jié)果婚禮上竟痰,老公的妹妹穿的比我還像新娘签钩。我一直安慰自己,他們只是感情好坏快,可當(dāng)我...
    茶點故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布铅檩。 她就那樣靜靜地躺著,像睡著了一般莽鸿。 火紅的嫁衣襯著肌膚如雪昧旨。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天富拗,我揣著相機(jī)與錄音臼予,去河邊找鬼。 笑死啃沪,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的窄锅。 我是一名探鬼主播创千,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼入偷!你這毒婦竟也來了追驴?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤疏之,失蹤者是張志新(化名)和其女友劉穎殿雪,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體锋爪,經(jīng)...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡丙曙,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年爸业,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片亏镰。...
    茶點故事閱讀 39,795評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡扯旷,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出索抓,到底是詐尸還是另有隱情钧忽,我是刑警寧澤,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布逼肯,位于F島的核電站耸黑,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏篮幢。R本人自食惡果不足惜大刊,卻給世界環(huán)境...
    茶點故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望洲拇。 院中可真熱鬧奈揍,春花似錦、人聲如沸赋续。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽纽乱。三九已至蛾绎,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間鸦列,已是汗流浹背租冠。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留薯嗤,地道東北人顽爹。 一個月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像骆姐,于是被迫代替她去往敵國和親镜粤。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,724評論 2 354

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