ViewGroup:布局也可以叫做容器醋旦,是承載控件的設(shè)備误证。
Android中的布局有很多種:
LinearLayout線性布局(橫著或豎著按順序排列)
RelativeLayout相對布局(起始坐標(biāo)時屏幕坐上角,以同級或上級為參考系定位位置)
FrameLayout幀布局(像千層餅一樣盖腿,一層壓著一層)
AbsoluteLayout絕對布局(以屏幕左上角為參考系爽待,定位自己的位置损同,從android 2.2版本后廢棄)
GridLayout網(wǎng)格布局(可以指定行數(shù)列數(shù),子控件自動根據(jù)行列數(shù)進行分配位置鸟款,于android 4.0后新增進api中)
TableLayout表格布局(類似于網(wǎng)格布局膏燃,以一個TableRow標(biāo)簽定義為一行或一列)
ConstraintLayout約束布局(google于2016年新發(fā)布的一種布局方式,以同級或上級的邊何什、點组哩、線為自己的限制的布局,它不在android的基礎(chǔ)api包里富俄,需要額外引入)
這些布局除了TableLayout是LinearLayout的子類外禁炒,其余都是ViewGroup的子類,包括LinearLayout,也可以說TableLayout是ViewGroup的孫類霍比。
一幕袱、LinearLayout
線性布局就是從左到右或從上到下按順序排列的一種布局。下面講一講LinearLayout的基礎(chǔ)屬性悠瞬。
(一)们豌、orientation:方向。
方向說的就是線性布局的方向了浅妆,也就是這個線性布局到底是水平方向逐個排列還是垂直方向逐個排列望迎。這個屬性有兩個值vertical(垂直排列),horizontal(水平排列)凌外。
默認值為horizontal
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
(二)辩尊、layout_width和layout_height:寬度、高度康辑。
layout_width和layout_height是android中控件的必要屬性摄欲,分別規(guī)定了控件的寬度和高度,這個兩個屬性的值可以是指定的值疮薇,也可以根據(jù)內(nèi)容自適應(yīng)胸墙,還可以填充整個剩余空間。
(1)match_parent:填充剩余窗體按咒。
(2)wrap_content:根據(jù)子控件的內(nèi)容大小自適應(yīng)迟隅。
(3)fill_parent:就是match_parent。
聽說從android 2.2以前是沒有match_parent的從android 2.2引入match_parent以后励七,fill_parent就定義為了match_parent智袭,之前的事我也不是太了解了,畢竟我接觸android開發(fā)時掠抬,已經(jīng)是android 4.4的時期了补履。
(4)自定義大小:例如50dp剿另、100px。
至于dp、px等單位雨女,以后單獨說谚攒。
二、效果
只看代碼和敘述可能比較難以理解氛堕,下面結(jié)合圖來理解LinearLayout馏臭。
<?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:background="#FF0000"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕3" />
</LinearLayout>
我定義了一個垂直的線性布局,里面有三個按鈕讼稚,按鈕以后再說括儒。它們的排列方式是這樣的。
如果把android:orientation屬性換成horizontal就變成了這樣
這回就更容易理解了锐想,一個是把內(nèi)部的按鈕水平排列帮寻,另一個是垂直排列。
再來看看match_parent和wrap_content的區(qū)別
<?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:background="#FF0000"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕3" />
</LinearLayout>
為了方便觀察赠摇,我把LinearLayout的背景設(shè)置了一個顏色固逗,這樣更方便看出它的大小。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#FF0000"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕3" />
</LinearLayout>
如果把layout_width換成wrap_content藕帜,寬度就不會填充滿整個屏幕了烫罩,就變成了和內(nèi)部最寬的控件一樣寬,高度也類似洽故。
另外贝攒,把layout_width設(shè)置成了200dp后就變成了上面這樣,它的寬度就被固定了时甚。