Android UI的四種基本布局

布局是一種可用于放置很多控件的容器,它可以按照一定的規律調整內部控件的位置,從而編寫出精美的界面。當然,布局的內部除了放置控件外,也可以放置布局,通過多層布局的嵌套,我們就能夠完成一些

比較復雜的界面實現,下圖很好地展示了它們之間的關系。


1 LinearLayout

LinearLayout 又稱作線性布局,是一種非常常用的布局。正如它名字所描述的一樣,這個布局會將它所包含的控件在線性方向上依次排列。

我們通過 android:orientation 屬性指定了排列方向是 vertical,如果指定的是 horizontal,控件就會在水平方向上排列了。

activity_main.xml 中的代碼,如下所示:

http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button 1" />

android:id="@+id/button2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button 2" />

android:id="@+id/button3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button 3" />

我 們 在 LinearLayout 中 添 加 了 三 個 Button , 每 個 Button 的 長 和 寬 都 是wrap_content,并指定了排列方向是vertical。現在運行一下程序,效果如下圖所示。

修改一下 LinearLayout 的排列方向,如下所示:

http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal">

??

將 android:orientation 屬性的值改成了 horizontal,這就意味著要讓 LinearLayout中的控件在水平方向上依次排列,當然如果不指定 android:orientation 屬性的值,默認的排列方向就是 horizontal。重新運行一下程序,效果如下圖所示。

這里需要注意,如果 LinearLayout 的排列方向是 horizontal,內部的控件就絕對不能將寬度指定為 match_parent,因為這樣的話單獨一個控件就會將整個水平方向占滿,其他的控件就沒有可放置的位置了。同樣的道理,如果 LinearLayout 的排列方向是 vertical,內部的控件就不能將高度指定為 match_parent。

幾個關鍵屬性

android:gravity是用于指定文字在控件中的對齊方式

android:layout_gravity是用于指定控件在布局中的對齊方式

android:layout_weight 這個屬性允許我們使用比例的方式來指定控件的大小,它在手機屏幕的適配性方面可以起到非常重要的作用。

我們還可以通過指定部分控件的layout_weight值,來實現更好的效果。修改activity_main.xml 中的代碼,如下所示:

http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal" >

android:id="@+id/input_message"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:hint="Type something"

/>

android:id="@+id/send"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Send"

/>

這里我們僅指定了 EditText 的 android:layout_weight 屬性,并將 Button 的寬度改回 wrap_content。這表示 Button 的寬度仍然按照 wrap_content 來計算,而 EditText則會占滿屏幕所有的剩余空間。使用這種方式編寫的界面,不僅在各種屏幕的適配方面會非常好,而且看起來也更加舒服,重新運行程序,效果如圖所示。

2RelativeLayout 又稱作相對布局

RelativeLayout 又稱作相對布局,也是一種非常常用的布局。和 LinearLayout 的排列規則不同,RelativeLayout 顯得更加隨意一些,它可以通過相對定位的方式讓控件出現在布局的任何位置。

android:layout_alignParentLeft

android:layout_alignParentTop

android:layout_alignParentRight

android:layout_alignParentBottom

android:layout_centerInParent

android:layout_alignLeft表示讓一個控件的左邊緣和另一個控件的左邊緣對齊

android:layout_alignRight表示讓一個控件的右邊緣和另一個控件的右邊緣對齊

android:layout_alignTop

android:layout_ alignBottom

activity_main.xml 中的代碼,如下所示:

http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:text="Button 1" />

android:id="@+id/button2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentRight="true"

android:layout_alignParentTop="true"

android:text="Button 2" />

android:id="@+id/button3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:text="Button 3" />

android:id="@+id/button4"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_alignParentLeft="true"

android:text="Button 4" />

android:id="@+id/button5"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_alignParentRight="true"

android:text="Button 5" />

我們讓 Button 1和父布局的左上角對齊,Button 2 和父布局的右上角對齊,Button 3 居中顯示,Button 4

和父布局的左下角對 齊,Button5和父布局的右下角對齊 。

上面例子中的每個控件都是相對于父布局進行定位的,那控件可不可以相對于控件進行定位呢?當然是可以的,修改 activity_main.xml 中的代碼,如下所示:

http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:id="@+id/button3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:text="Button 3" />

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_above="@id/button3"

android:layout_toLeftOf="@id/button3"

android:text="Button 1" />

android:id="@+id/button2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_above="@id/button3"

android:layout_toRightOf="@id/button3"

android:text="Button 2" />

android:id="@+id/button4"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/button3"

android:layout_toLeftOf="@id/button3"

android:text="Button 4" />

android:id="@+id/button5"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/button3"

android:layout_toRightOf="@id/button3"

android:text="Button 5" />

次的代碼稍微復雜一點,不過仍然是有規律可循的。android:layout_above 屬性可以讓一個控件位于另一個控件的上方,需要為這個屬性指定相對控件 id 的引用,這里我們填入了@id/button3,表示讓該控件位于 Button 3 的上方。其他的屬性也都是相似的,android:layout_below 表 示 讓一個控件位于另一個控件的下方 ,android:layout_toLeftOf 表示讓一個控件位于另一個控件的左側,android:layout_toRightOf 表示讓一個控件位于另一個控件的右側。注意,當一個控件去引用另一個控件的 id 時,該控件一定要定義在引用控件的后面,不然會出現找不到 id 的情況。

3 FrameLayout

FrameLayout 相比于前面兩種布局就簡單太多了,因此它的應用場景也少了很多。這

種布局沒有任何的定位方式,所有的控件都會擺放在布局的左上角。

4TableLayout

TableLayout 允許我們使用表格的方式來排列控件,這種布局也不是很常用,你只需要了解一下它的基本用法就可以了。既然是表格,那就一定會有行和列,在設計表格時我們盡量應該讓每一行都擁有相同的列數,這樣的表格也是最簡單的。不過有時候事情并非總會順從我們的心意,當表格的某行一定要有不相等的列數時,就需要通過合并單元格的方式來應對。

5AbsoluteLayout

Android 中其實還有一個 AbsoluteLayout,不過這個布局官方已經不推薦使用了

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,237評論 6 537
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,957評論 3 423
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,248評論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,356評論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,081評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,485評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,534評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,720評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,263評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,025評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,204評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,787評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,461評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,874評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,105評論 1 289
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,945評論 3 395
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,205評論 2 375

推薦閱讀更多精彩內容