android常用五大布局
android的佈局方式有五種,分別是:LinearLayout(線性佈局)、FrameLayout(單幀佈局)、RelativeLayout(相對佈局)、AbsoluteLayout(絕對佈局)和TableLayout(表格佈局)。
android的佈局之間可以互相嵌套。
所有的佈局方式都可以歸類爲ViewGroup的5個類別,即ViewGroup的5個直接子類(即:所說的五種佈局方式)。其它的一些佈局都擴展自這5個類。
操作方法
(01)LinearLayout(線性佈局):這種佈局比較常用,也比較簡單,就是每個元素佔一行,當然也可能聲明爲橫向排放,也就是每個元素佔一列。LinearLayout按照垂直或者水平的順序依次排列子元素,每一個子元素都位於前一個元素之後。如果是垂直排列,那麼將是一個N行單列的結構,每一行只會有一個元素,而不論這個元素的寬度爲多少;如果是水平排列,那麼將是一個單行N列的結構。如果搭建兩行兩列的結構,通常的方式是先垂直排列兩個元素,每一個元素裏再包含一個LinearLayout進行水平排列。LinearLayout中的子元素屬性android:layout_weight生效,它用於描述該子元素在剩餘空間中佔有的大小比例。加入一行只有一個文字方塊,那麼它的默認值就爲0,如果一行中有兩個等長的文字方塊,那麼他們的android:layout_weight值可以是同爲1。如果一行中有兩個不等長的文字方塊,那麼他們的android:layout_weight值分別爲1和2,那麼第一個文字方塊將佔據剩餘空間的三分之二,第二個文字方塊將佔據剩餘空間中的三分之一。android:layout_weight遵循數值越小,重要度越高的原則。<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:background="#ff000000"android:text="@string/hello"/><LinearLayoutandroid:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:background="#ff654321"android:layout_weight="1"android:text="1"/><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:background="#fffedcba"android:layout_weight="2"android:text="2"/></LinearLayout></LinearLayout>
(02)FrameLayout(單幀佈局):FrameLayout是五大布局中最簡單的一個佈局,可以說成是層佈局方式。在這個佈局中,整個介面被當成一塊空白備用區域,所有的子元素都不能被指定放置的位置,它們統統放於這塊區域的左上角,並且後面的子元素直接覆蓋在前面的子元素之上,將前面的子元素部分和全部遮擋。如下,第一個TextView被第二個TextView完全遮擋,第三個TextView遮擋了第二個TextView的部分位置。<?xml version="1.0" encoding="utf-8"?> <FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:background="#ff000000"android:gravity="center"android:text="1"/><TextViewandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:background="#ff654321"android:gravity="center"android:text="2"/><TextViewandroid:layout_width="50dp"android:layout_height="50dp"android:background="#fffedcba"android:gravity="center"android:text="3"/></FrameLayout>
(03)RelativeLayout(相對佈局):RelativeLayout按照各子元素之間的位置關係完成佈局。在此佈局中的子元素裏與位置相關的屬性將生效。例如android:layout_below, android:layout_above, android:layout_centerVertical等。注意在指定位置關係時,引用的ID必須在引用之前,先被定義,否則將出現異常。RelativeLayout是Android五大布局結構中最靈活的一種佈局結構,比較適合一些複雜介面的佈局。<?xml version="1.0" encoding="utf-8"?> <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:id="@+id/text_01"android:layout_width="50dp"android:layout_height="50dp"android:background="#ffffffff"android:gravity="center"android:layout_alignParentBottom="true"android:text="1"/><TextViewandroid:id="@+id/text_02"android:layout_width="50dp"android:layout_height="50dp"android:background="#ff654321"android:gravity="center"android:layout_above="@id/text_01"android:layout_centerHorizontal="true"android:text="2"/><TextViewandroid:id="@+id/text_03"android:layout_width="50dp"android:layout_height="50dp"android:background="#fffedcba"android:gravity="center"android:layout_toLeftOf="@id/text_02"android:layout_above="@id/text_01"android:text="3"/></RelativeLayout>
(04)AbsoluteLayout(絕對佈局):在此佈局中的子元素的android:layout_x和android:layout_y屬性將生效,用於描述該子元素的座標位置。屏幕左上角爲座標原點(0,0),第一個0代表橫座標,向右移動此值增大,第二個0代表縱座標,向下移動,此值增大。在此佈局中的子元素可以相互重疊。在實際開發中,通常不採用此佈局格式,因爲它的介面代碼過於剛性,以至於有可能不能很好的適配各種終端。<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:layout_width="50dp"android:layout_height="50dp"android:background="#ffffffff"android:gravity="center"android:layout_x="50dp"android:layout_y="50dp"android:text="1"/><TextViewandroid:layout_width="50dp"android:layout_height="50dp"android:background="#ff654321"android:gravity="center"android:layout_x="25dp"android:layout_y="25dp"android:text="2"/><TextViewandroid:layout_width="50dp"android:layout_height="50dp"android:background="#fffedcba"android:gravity="center"android:layout_x="125dp"android:layout_y="125dp"android:text="3"/></AbsoluteLayout>
(05)TableLayout(表格佈局):適用於N行N列的佈局格式。一個TableLayout由許多TableRow組成,一個TableRow就代表TableLayout中的一行。TableRow是LinearLayout的子類,ablelLayout並不需要明確地聲明包含多少行、多少列,而是透過TableRow,以及其他組件來控制表格的行數和列數, TableRow也是容器,因此可以向TableRow裏面添加其他組件,沒添加一個組件該表格就增加一列。如果想TableLayout裏面添加組件,那麼該組件就直接佔用一行。在表格佈局中,列的寬度由該列中最寬的單元格決定,整個表格佈局的寬度取決於父容器的寬度(默認是佔滿父容器本身)。TableLayout繼承了LinearLayout,因此他完全可以支援LinearLayout所支援的全部XML屬性,除此之外TableLayout還支援以下屬性:1、XML屬性:andriod:collapseColumns相關用法:setColumnsCollapsed(int,boolean)說 明:設定需要隱藏的列的序列號,多個用逗號隔開2、XML屬性:android:shrinkColumns相關用法:setShrinkAllColumns(boolean)說 明:設定被收縮的列的序列號,多個用逗號隔開3、XML屬性:android:stretchColimns相關用法:setSretchAllColumnds(boolean)說 明:設定允許被拉伸的列的序列號,多個用逗號隔開<?xml version="1.0" encoding="utf-8"?> <TableLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TableRowandroid:layout_width="fill_parent"android:layout_height="wrap_content"><TextViewandroid:background="#ffffffff"android:gravity="center"android:padding="10dp"android:text="1"/><TextViewandroid:background="#ff654321"android:gravity="center"android:padding="10dp"android:text="2"/><TextViewandroid:background="#fffedcba"android:gravity="center"android:padding="10dp"android:text="3"/></TableRow><TableRowandroid:layout_width="fill_parent"android:layout_height="wrap_content"><TextViewandroid:background="#ff654321"android:gravity="center"android:padding="10dp"android:text="2"/><TextViewandroid:background="#fffedcba"android:gravity="center"android:padding="10dp"android:text="3"/></TableRow><TableRowandroid:layout_width="fill_parent"android:layout_height="wrap_content"><TextViewandroid:background="#fffedcba"android:gravity="center"android:padding="10dp"android:text="3"/><TextViewandroid:background="#ff654321"android:gravity="center"android:padding="10dp"android:text="2"/><TextViewandroid:background="#ffffffff"android:gravity="center"android:padding="10dp"android:text="1"/></TableRow></TableLayout>
特別提示
圖片可以與文字中代碼段不符,但是對於理解佈局可以做到一個很好的參考
本文圖片來自於http://www.cnblogs.com/中的“熊貓82”,文字和代碼部分來源於http://www.cnblogs.com/devinzhang/archive/2012/01/19/2327535.html
-
《NBA 2K Mobile籃球》手機版下載安裝方式
喜歡看NBA的朋友一定對NBA2KMobile籃球手遊不會陌生,其豐富新穎的玩法,超高的自由度,都讓人慾罷不能,在這款遊戲裏我們不僅能操縱現役NBA球員,還能自己帶隊操縱歷史名宿。真實場景還原能讓我們在閒暇的時間裏足不出戶就完成籃球夢。不過很多朋友都不知道在哪裏下載,...
-
華爲手機怎麼開啓/禁止後臺自動執行應用
簡介我們的手機平時用起來感覺會卡頓,這是因爲手機後臺自動執行程序過多,很多人不知道怎麼回事,今天小編教大家怎麼關閉開啓或禁止後臺自動執行應用。操作方法(01)在此以努比亞手機爲例。(02)首先開啟手機,找到桌面上的【智能管理器】,點擊開啟。(03)點擊正下方的【自動運...
-
手機如何截取視頻
方法一:手機相冊自帶功能(01)在手機桌面上找到【相冊】功能,點擊開啟。(02)接着,在相冊的“圖集”分類下找到【視頻】存放集。(03)開啟視頻存放集,找到你想要截取的視頻,點擊開啟它。(04)進入視頻的播放頁面後,在下方工具欄中找到並點擊【編輯】。(05)然後在編輯頁面點擊【剪切...
-
鬥魚粉絲熒光棒怎麼領
鬥魚成爲主播粉絲,獲得粉絲勳章每天都可以領取熒光棒,具體該怎麼操作呢?操作方法(01)進入鬥魚直播,找到喜歡的主播直播間進入,點擊主播。(02)下方選擇禮物盒子的標誌進入。(03)在禮物選項裏先選擇寵愛卡送給主播,然後可以獲得主播的粉絲勳章。(04)以後每天點擊禮物盒子,選擇道...