概述
佈局(Layout)的概念是針對Activity的,Activity就是佈滿整個Android設備的窗口或者懸浮於其他窗口上的交互界面。在一個應用程序中通常由多個Activity構成,每個需要顯示的Activity都需要在AndroidManifest.xml文件之中聲明。
通常情況下,開發人員可以使用兩種方式來創建UI組件,一種方式是使用XML方式來配置UI組件的相關屬性,然後裝載這些UI組件,這也是最常用的方式。但是有些特殊情況下,需要動態生成UI組件,則需要使用第二種方式,完全使用Java代碼來創建UI組件。
XML佈局文件是Android系統中定義的Layout的常用方式,所有佈局文件必須包含在res/layout目錄中,且必須符合Java的命名規範。當在res/layout目錄下新增了佈局文件之後,R.java文件會自動收錄該佈局資源,Java代碼可通過setContentView方法在Activity中顯示該Layout。
setContentView(R.layout.<資源名稱>);
Android應用的絕大部分UI組件都放在android.widget包及其子包、android.view包及其子包中,Android應用的所有UI組件都繼承了View類。View類還有一個重要的子類:ViewGroup,ViewGroup類是所有佈局管理器的父類。
ViewGroup容器控制其子組件的分佈依賴於ViewGroup.LayoutParams、ViewGroup.MarginLayoutParams兩個內部類。
ViewGroup.LayoutParams提供兩個XML屬性設定組件的大小。
- android:layout_height:指定該子組件的基本高度;
- android:layout_width:指定該子組件的基本寬度。
- fill_parent:指定組件的高度、寬度與父容器組件的一樣。
- match_parent:與fill_parent一樣,Android2.2開始推薦使用。
- warp_content:內容包裹。
- android:layout_marginBottom(下邊距);
- android:layout_marginLeft(左邊距);
- android:layout_marginRight(右邊距):
- layout_marginTop(上邊距)
對於View的尺寸,android提供了三種單位供選擇使用:
- px:像素。
- dp:dpi,表示屏幕實際的像素。
- sp:會根據系統設定的字體大小進行縮放,與dp類似,通常用於設置字體。
尺寸單位選擇的技巧:如果設置長度、高度等屬性時可以使用dp,但是如果設置字體,推薦使用sp。在屏幕適配方面,不推薦使用px,因爲使用dp或者sp,UI會根據設備的density和scaledDensity進行等比例縮放,以達到不同屏幕適配的功效,讓其在不同的屏幕上看起來差不多的效果。
爲了適應各種界面風格,Android提供了五種佈局規範,利用這五種佈局,基本上可以在設備上隨心所欲的擺放任何UI組件,這五種佈局分別是:
- FrameLayout(幀佈局)。
- LinearLayout(線性佈局)
- RelativeLayout(相對佈局)。
- TableLayout(表格佈局)。
- AbsoluteLayout(絕對佈局)。
線性佈局(LinearLayout)
LinearLayout是最常用的佈局方式,在XML文件中使用<LinearLayout>標記。它會將容器裏的UI組件一個一個挨着排列起來。但是LinearLayout不會換行,當UI組件超出屏幕之後,則不會被顯示出來。LinearLayout有兩個重要的XML屬性:android:gravity(對齊方式);android:orientation(排列方式)。
android:orientation(排列方式),設定了LinearLayout中包含的UI組件的排列方式,有兩個選項vertical(豎向)、horizontal(橫向,默認值)
android:gravity(對齊方式),設定LinearLayout中包含UI組件的對齊方式,其選項很多,常用上(top)、下(bottom)、左(left)、右(right)。
LinearLayout佈局示例:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" android:orientation="horizontal" android:gravity="bottom">
<Button
android:layout_weight="1"
android:text="button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="button"
android:layout_gravity="top"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="button"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
顯示效果:幀佈局(FrameLayout)
幀佈局是最簡單的佈局方式,所有添加到這個佈局中的視圖都是以層疊的方式顯示,並且後聲明的遮擋先聲明的控件。
幀佈局容器爲每個加入其中的組件創建一個空白的區域(稱爲一幀),所有每個子組件佔據一幀,這些幀都會根據gravity屬性執行自動對齊。
下面通過一個例子說明幀佈局,以一個經典的漸變色條爲例,在FrameLayout中以此添加多個TextView,賦予不同尺寸和顏色,會疊加顯示。
FrameLayout示例代碼:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="210px"
android:height="50px"
android:background="#ff0000"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="180px"
android:height="50px"
android:background="#dd0000"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="150px"
android:height="50px"
android:background="#bb0000"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="120px"
android:height="50px"
android:background="#990000"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="90px"
android:height="50px"
android:background="#770000"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="60px"
android:height="50px"
android:background="#550000"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="30px"
android:height="50px"
android:background="#330000"
/>
</FrameLayout>
以上示例顯示效果圖:相對佈局(RelativeLayout)
RelativeLayout,其內子組件的位置總是相對兄弟UI組件、父親容器來決定的。比如UI組件A相對於UI組件B的位置進行定位,那麼UI組件B需要在UI組件A之前定義。
相對佈局用到的主要屬性:
- android:layout_below:在某元素的下方。
- android:layout_above:在某元素的上方。
- android:layout_toLeftOf:在某元素的左邊。
- android:layout_toRightOf:在某元素的右邊。
- android:layout_alignXxx:控制與某元素的邊界對其方式。
下面通過一個例子來說明RelativeLayout,在該佈局XML中,確定一個經典的梅花布局效果,上下左右的UI組件通過中間的UI組件位置確定自己的位置。
示例代碼:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中"
android:layout_centerInParent="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上"
android:layout_above="@id/view1"
android:layout_alignLeft="@id/view1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下"
android:layout_below="@id/view1"
android:layout_alignLeft="@id/view1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左"
android:layout_toLeftOf="@id/view1"
android:layout_alignTop="@id/view1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="右"
android:layout_toRightOf="@id/view1"
android:layout_alignTop="@id/view1"
/>
</RelativeLayout>
效果展示表格佈局(TableLayout)
表格佈局,採用行、列的形式來管理UI組件,TableLayout通過TableRow、其他UI組件來控制表格的行數和列數。
每次向TableLayout中添加一個TableRow,該TableRow就是一個表格行,TableRow也是容器,因此它也可以不斷添加其他組件,沒添加一個子組件,該表格就增加一列。如果直接向TableLayout中添加組件,那麼這個組件將直接佔用一行。
- android:collapseColumns:設置需要被隱藏的列序號。
- android:shrinkColumns:設置需要被收縮的列序號。
- android:stretchColumns:設置需要被拉伸的列序號。
注意:TableLayout中所謂的序列號是從0開始計算的。
下面用一個例子來說明TableLayout佈局,代碼如下:
<?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" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1,2,3" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button" />
</TableRow>
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00ffff" />
</TableLayout>
</LinearLayout>
絕對佈局(AbsoluteLayout)
對於AbsoluteLayout,android不提供任何佈局控制,而是由開發人員自己通過X座標、Y座標來控制組件的位置。
在AbsoluteLayout中,每個子組件都需要通過兩個XML屬性來確定座標:layout_x:指定該子組件的X座標;layout_y:指定該子組件的Y座標。
因爲此佈局比較繁瑣,而且在不同的屏幕上顯示效果差距比較大,所以一般不推薦使用,下面通過一個簡單的示例來展示AbsoluteLayout的用法
示例代碼:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="84dp"
android:layout_y="20dp"
android:ems="10" />
<EditText
android:id="@+id/EditText01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="84dp"
android:layout_y="66dp"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="214dp"
android:layout_y="113dp"
android:text="重置" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="130dp"
android:layout_y="113dp"
android:text="登錄" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="33dp"
android:layout_y="36dp"
android:text="用戶名" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="48dp"
android:layout_y="80dp"
android:text="密碼" />
</AbsoluteLayout>
展示效果:文章來源:https://www.cnblogs.com/plokmju/p/androidUI_Layout.html