Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 資訊

Android 五大布局講解與應用

編輯:Android 資訊

本文由碼農網 – 楊小勇原創,轉載請看清文末的轉載要求,歡迎參與我們的付費投稿計劃!

Android 總體上有五大布局:

  • 線性布局(LinearLayout):屏幕垂直或水平方向布局。
  • 幀布局(FrameLayout):控件從屏幕左上角開始層疊布局。
  • 相對布局(RelativeLayout):以其他控件為參照布局。
  • 絕對布局(AbsoluteLayout):以屏幕坐標布局。
  • 表格布局(TableLayout):按行列方式布局。

一、LinearLayout

線性布局在開發中使用最多,具備垂直與水平兩種方向,通過 android:orientation 控制,取值 vertical 或 horizontal,默認水平。

使用 orientation 時要注意一個問題:若採用默認的水平方向,且存在不止一個未設置具體寬度的子控件,例如:

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>

就會出現下面的錯誤:

這告訴我們:有多個子控件時需設置布局方向,或至少設置某個子控件在該方向上的大小。除 orientation 外,還有幾個常用屬性:

android:gravity:內部控件對齊方式,常用值有 center、center_vertical、center_horizontal、top、bottom、left、right 等。此屬性在 RelativeLayout、TableLayout 中也有,FrameLayout、AbsoluteLayout 則沒有。center 並非顯示在 LinearLayout 中心:垂直方向時等同 center_horizontal,水平方向時等同 center_vertical。

要與 android:layout_gravity 區分開:layout_gravity 用來設置自身相對於父元素的布局。

android:layout_weight:權重,用來分配當前控件在剩餘空間的大小,一般值越大佔據越多。但有個例外:水平布局且子控件寬度為 fill_parent/match_parent 時,值越小佔據反而越大。分析如下:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="40dp" android:background="#666666" android:text="第一個子控件" android:gravity="center" android:layout_weight="1"/> <TextView android:layout_width="match_parent" android:layout_height="40dp" android:background="#EE4000" android:text="第二個子控件" android:gravity="center" android:layout_weight="2"/> </LinearLayout>

顯示效果:

之所以如此,是因為 match_parent 引起的:系統先給第一個子控件分配 parent_width,再給第二個分配 parent_width,共分出去兩個 parent_width,此時剩餘為 -parent_width(負數)。接著第一個控件佔寬 = parent_width + 1/3×(-parent_width) = 2/3 parent_width;第二個佔寬 = parent_width + 2/3×(-parent_width) = 1/3 parent_width。可見寬度都是 match_parent 時剩餘空間為負,誰的權重大誰就被減去越多。日常開發中善用 layout_weight 能解決很多看似不可思議的布局問題。

二、FrameLayout

幀布局也叫層布局,從屏幕左上角按層次堆疊,後面的控件覆蓋前面的。由於是層疊布局,實現層面顯示的樣式時常會用到它。比如要實現一個類似百度地圖的布局:

可以看到地圖與操作按鈕是分層顯示的,用 FrameLayout 也能簡單實現:

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.baidu.mapapi.map.MapView android:id="@+id/mapShowView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true"/> <EditText android:layout_width="match_parent" android:layout_height="40sp" android:background="@drawable/edit_selector" android:layout_marginLeft="20sp" android:layout_marginRight="20sp" android:layout_marginTop="30dp" android:paddingLeft="10sp" android:textSize="14sp" android:hint="搜地點、查公交、找路線" android:textColorHint="#aaaaaa" android:gravity="left|center_vertical"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_gravity="right" android:layout_marginTop="80dp" android:layout_marginRight="20dp"> <Button android:id="@+id/traffic" android:layout_width="45sp" android:layout_height="45sp" android:text="路況" android:textColor="#ffffff" android:textSize="14sp" android:background="@drawable/corner_black_bgborder"/> <Button android:id="@+id/panorama" android:layout_width="45sp" android:layout_height="45sp" android:text="全景" android:textColor="#ffffff" android:textSize="14sp" android:layout_marginTop="10dp" android:background="@drawable/corner_black_bgborder"/> </LinearLayout> <Button android:id="@+id/location" android:layout_width="45sp" android:layout_height="45sp" android:layout_gravity="bottom" android:text="定位" android:textColor="#ffffff" android:textSize="14sp" android:layout_marginLeft="20dp" android:layout_marginBottom="120dp" android:background="@drawable/corner_black_bgborder"/> </FrameLayout>

顯示效果:

第一個子控件為百度地圖,其次為輸入框、右側操作按鈕與左下方定位按鈕。這裡很多子控件都用到了 layout_gravity,用來控制控件在布局中的位置,常用值有 top、bottom、left、right、center、center_horizontal、center_vertical,可複合使用,如 android:layout_gravity=bottom|center_vertical。三部分的講解如下:

  1. 搜索輸入框:子控件默認從左上角堆疊,這裡設置上、左、右方向的 margin 即可顯示在上方;通過寬度 match_parent 加左右相同 margin 實現水平居中,也可設固定寬度後用 layout_gravity=center_horizontal。
  2. 右側操作按鈕欄:三個按鈕組成垂直 LinearLayout,設置 android:layout_gravity=right 靠右,再配合 margin 合理排布。
  3. 定位按鈕:顯示於左下方,設置 android:layout_gravity=bottom 再加適當 margin 即可。

三、RelativeLayout

相對布局讓子控件相對於兄弟控件或父控件布局,可設置上下左右對齊。它能替換一些嵌套視圖——當你用 LinearLayout 實現簡單布局卻用了過多嵌套時,就可考慮改用 RelativeLayout 重新布局。

RelativeLayout 中子控件常用屬性:

1、相對於父控件,如 android:layout_alignParentTop="true":

layout_alignParentTop 頂部與父控件頂部對齊;layout_alignParentBottom 底部對齊;layout_alignParentLeft 左部對齊;layout_alignParentRight 右部對齊。

2、相對於給定 Id 控件,如 android:layout_above="@id/**":

layout_above 置於給定 ID 之上;layout_below 置於其下;layout_toLeftOf 右緣與給定 ID 左緣對齊;layout_toRightOf 左緣與給定 ID 右緣對齊;layout_alignBaseline 基線對齊;layout_alignTop/Bottom/Left/Right 各邊緣對齊。

3、居中,如 android:layout_centerInParent="true":

layout_centerHorizontal 水平居中;layout_centerVertical 垂直居中;layout_centerInParent 位於父控件中央。

應用實例:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="@drawable/topbar_bg_border"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="標題" android:textSize="19sp" android:textColor="#4e6288"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:layout_marginRight="15dp" android:text="取消" android:textSize="18sp"/> </RelativeLayout> </LinearLayout>

顯示結果:

這是一個簡單的頂部布局樣式,很多 App 都會用統一風格的頂部。這裡用 RelativeLayout 更簡單合理:設置其高度後,讓 Title 的 layout_centerInParent="true" 居中,取消按鈕同時設置 layout_centerVertical="true" 與 layout_alignParentRight="true"。

四、AbsoluteLayout

絕對布局也叫坐標布局,直接指定控件的絕對位置,簡單直觀,但手機屏幕尺寸差異大、適應性差,Android 1.5 已棄用,可用 RelativeLayout 替代。

<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="100dip" android:layout_height="120dip" android:layout_x="150dip" android:layout_y="40dip" android:src="@drawable/android_logo"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="100dip" android:layout_y="150dip" android:text="上一張"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="200dip" android:layout_y="150dip" android:text="下一張"/> </AbsoluteLayout>

顯示效果:

這裡為了做圖片浏覽效果,把各控件的 layout_x 與 layout_y 逐一設了一遍。當前屏幕能正常顯示,換個屏幕就得重新定制布局。其實用 RelativeLayout 輕鬆就能實現,還不用考慮兼容性。所以 AbsoluteLayout 已成為 Android 布局中的歷史。

五、TableLayout

表格布局繼承自 LinearLayout,通過 TableRow 設置行,列數由 TableRow 中的子控件決定,直接在 TableLayout 中添加子控件會佔據整行。

TableLayout 常用屬性:

android:shrinkColumns:設置可收縮的列;android:stretchColumns:設置可伸展的列,將空白填充滿整列;android:collapseColumns:設置要隱藏的列。列的索引從 0 開始,shrinkColumns 與 stretchColumns 可同時設置。

子控件常用屬性:

android:layout_column:第幾列;android:layout_span:佔據列數。

TableLayout 實例代碼:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="首頁"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center"> <TableLayout android:layout_width="match_parent" android:layout_height="match_parent" android:stretchColumns="0,1,2" android:gravity="center"> <TableRow> <TextView android:layout_width="100dp" android:layout_height="100dp" android:layout_margin="5dp" android:background="#e2a617" android:text="文件管理" android:gravity="center"/> <TextView android:layout_width="100dp" android:layout_height="100dp" android:layout_margin="5dp" android:background="#0d637f" android:text="應用商店" android:gravity="center"/> <TextView android:layout_width="100dp" android:layout_height="100dp" android:layout_margin="5dp" android:background="#aa2266" android:text="文件管理" android:gravity="center"/> </TableRow> <TableRow> <TextView android:layout_width="100dp" android:layout_height="100dp" android:layout_margin="5dp" android:background="#45e15f" android:text="應用管理" android:gravity="center"/> <TextView android:layout_width="200dp" android:layout_height="100dp" android:layout_margin="5dp" android:background="#3924a4" android:text="應用中心" android:gravity="center" android:layout_span="2"/> </TableRow> </TableLayout> </LinearLayout> <TableLayout android:layout_width="match_parent" android:layout_height="55dp" android:background="#f5f5f5" android:stretchColumns="0,1,2,3" android:gravity="center_vertical"> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="首頁" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="消息" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="發現" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="我" /> </TableRow> </TableLayout> </LinearLayout>

顯示效果:

屏幕中心是一個類似 Material 的布局,底部是頁面切換的導航欄。底部通過 stretchColumns="0,1,2,3" 讓四個按鈕等寬並填充整個寬度;中心區域主要用 stretchColumns="0,1,2" 填充顯示,並用 layout_span="2" 控制大內容跨列顯示。

布局相當於界面的框架。開發中只有了解各種布局的顯示樣式與彼此差異,才能駕輕就熟、合理地把界面布局出來。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved