【Android開發-5】界面裝修,五大布局你選誰
前言:假設要開一家店,門店裝修是非常重要的事情。有錢都請專門的建築設計公司來設計裝修,沒錢的僅僅能自己瞎折騰。好不好看全憑自己的感覺。像Android開發。在移動端大家看到的界面視覺不咋滴,一般連打開的動力都沒了。所以Android開發就有了專門的UI設計人員,既然有了UI設計圖,那怎麽布局就須要靠自己去選擇了,五大布局中能夠任意選,僅僅要能達到你的UI設計圖的效果。
設計圖給你了,你選哪位裝修工給你裝修,就看效率了;不用說。我們都選擇效率高的來裝修。
Android的五大布局:
1.線性布局(LinearLayout)
2.相對布局(RelativeLayout)
3.幀布局(FrameLayout)
4.表格布局(TableLayout)
5.絕對布局(AbsoluteLayout)
一、線性布局
首先直接看效果圖:
接著看界面布局文件裏的代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#aa0000" android:text="線性布局實戰--垂直效果" android:layout_gravity="center" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#00ff00" android:text="線性布局實戰--垂直效果" android:gravity="center" /> <LinearLayout android:layout_width="match_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="#FFC0CB" android:text="線性布局實戰--垂直效果" android:layout_gravity="center" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="#b0e0e6" android:text="線性布局實戰--垂直效果" android:layout_gravity="center" android:layout_weight="1" /> </LinearLayout> </LinearLayout>
感性分析理解下:
LinearLayout中的android:orientation是線性布局中非常重要的一個屬性,通過設置兩個屬性值:vertical和horizontal,它能夠讓包括在LinearLayout中的子控件依照垂直或者水平方向來布局。
上面還實用到布局中經常使用的屬性。我們也先感性的認識下:
android:layout_width -- 設置布局寬度
android:layout_height -- 設置布局高度(這兩個屬性。它的值能夠設置有match_parent、wrap_content、fill_parent,分別代表匹配父控件高度或寬度。包裹內容的高度或寬度、充滿父控件,效果圖中能夠看到對應效果)
android:layout_gravity -- 設置控件的位置,它的屬性值有center/right/left等
android:gravity -- 設置控件中內容位置,它的屬性值有center/right/left等
android:background
-- 設置背景色
android:layout_weight
-- 設置控件在LinearLayout中的所占的相對大小比例或者說權重值,這個要大家實踐多個控件各種比例,才幹更好理解
android:text
-- 設置控件的文本值
註:對於線性布局。當然還能夠嵌套各種布局,上面就線性布局嵌套了線性布局。事實上各種布局能夠互相嵌套,僅僅要沒出錯即可。實踐出真理。
二、相對布局
效果圖:
界面布局文件裏的代碼:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/first" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:background="#E9967A" android:text="相對布局實戰--控件居中" /> <TextView android:id="@+id/second" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/first" android:background="#FFC0CB" android:gravity="center" android:text="相對布局實戰--內容居中" /> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/second" > <EditText android:id="@+id/third" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:hint="相對布局嵌套實戰" /> <Button android:id="@+id/fouth" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@id/third" android:text="相對父控件右對齊" /> <Button android:id="@+id/fifth" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/third" android:layout_toLeftOf="@id/fouth" android:text="兄弟控件的左邊" /> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/fifth" > <View android:id="@+id/strut" android:layout_width="0dp" android:layout_height="0dp" android:layout_centerHorizontal="true" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignRight="@id/strut" android:text="控件水平居中" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_alignLeft="@id/strut" android:layout_alignParentRight="true" android:text="控件水平居中" /> </RelativeLayout> </RelativeLayout> </RelativeLayout>
感性分析理解下:
相對布局簡單理解就是有個參照物讓你參考,比方你相對於參照物1在左邊,可是相對於參照物2卻在右邊。
所以你選擇的參照物不同,取得的結果也就不一樣。
相對布局中,有接觸到新屬性例如以下:
android:id -- 設置控件的id,這裏設置的id,編譯器會自己主動把它生成在前面介紹過的R.java中
android:layout_centerHorizontal -- 布局中控件水平居中。屬性值true/false
android:layout_below -- 相對XX控件的下方。屬性值需設置相對XX控件的id
android:layout_toLeftOf -- 設置XX控件的左邊。屬性值需設置對於XX控件的id
android:layout_alignParentRight -- 相對父控件的右邊,屬性值true/false
相對布局的控件屬性還有非常多。可是基本上都是上面的。僅僅是相對位置不同,這個翻翻手冊就能夠了解。還有從上面能夠看出線性布局有時候能做的。相對布局也能做。
所以實踐中,誰方便就用誰。沒強求。
三.幀布局
效果圖:
界面布局文件裏的代碼:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="300dp" android:layout_height="300dp" android:background="#00BFFF" android:gravity="right" android:text="一層" /> <TextView android:layout_width="260dp" android:layout_height="260dp" android:background="#FFC0CB" android:gravity="right" android:text="二層" /> <TextView android:layout_width="220dp" android:layout_height="220dp" android:background="#F7EED6" android:gravity="right" android:text="三層" /> <Button android:layout_width="100dp" android:layout_height="100dp" android:text="四層" /> </FrameLayout>
感性分析理解下:
幀布局能夠簡單理解成一個洋蔥。一層層的堆疊,你撥開一層,就能夠看見裏面的一層。每一個控件在它裏面都是相當於一層。
假設裏面嵌套個線性布局,也相當於一層。不信大家能夠實踐。
幀布局非常重要的就是:它的位置都是從屏幕左上角(0。0)開始布局,然後覆蓋的順序是按你寫的控件順序,最先寫的在最以下層。相當於洋蔥的最裏層。
四、表格布局
效果圖:
界面布局文件裏的代碼:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:shrinkColumns="0" android:stretchColumns="0,1,2" > <TableRow> <Button android:layout_column="0" android:text="第一行" /> <Button android:layout_column="1" android:text="第一行" /> </TableRow> <TableRow> <Button android:layout_column="0" android:text="第二行" /> <Button android:layout_column="2" android:text="第二行" /> </TableRow> <TableRow> <Button android:layout_column="1" android:text="第三行" /> </TableRow> <TableRow> <Button android:layout_column="1" android:layout_span="2" android:text="第四行" /> </TableRow> </TableLayout>
感性分析理解下:
表格這個顧名思義,就非常easy理解。
不就是一張表格嘛。表格主要就是行和列,所以表格布局,懂得怎麽設置行和列,基本搞定。
表格布局中。屬性例如以下:
android:shrinkColumns -- 查shrink英文單詞,是收縮的意思。所以它就是列裏面內容非常多時。它就收縮,屬性值設置指定列
android:stretchColumns -- 伸縮行,盡量占滿界面,屬性值設置指定列。比方1,2。3
android:layout_column -- 設置指定列,比方是0,還是1,還是2
android:layout_span -- 設置一個霸占總數的幾列。你設置2,總數為3。就是當前列獨享2列的寬度
TableRow -- 設置行
五、絕對布局
效果圖:
界面布局文件裏的代碼:
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="20dip" android:layout_y="20dip" android:text="帳號:" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="80dip" android:layout_y="15dip" android:width="210px" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="20dip" android:layout_y="80dip" android:text="密 碼:" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="80dip" android:layout_y="75dip" android:password="true" android:width="210px" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="130dip" android:layout_y="135dip" android:text="登 錄" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="220dip" android:layout_y="135dip" android:text="重 置" /> </AbsoluteLayout>
感性分析理解下:
絕對布局能夠這樣簡單理解,你擁有絕對的權利,你想在界面哪個位置放控件,就能夠在哪個位置放控件。
絕對布局最重要的助手:
android:layout_x="xxdip"
android:layout_y="xxdip"
通過這兩個助手。你就能夠命令界面中的控件在哪個位置呆著,就在哪個位置呆著。
最終五大布局,弄完了。自己通過實踐也進一步加深理解,總的說有收獲!
【Android開發-5】界面裝修,五大布局你選誰