跟我學android-android常用布局介紹


在上一章我們曾經談到,Android平台的界面 是使用XML的方式設計的,然后在上一章我們只做了一個簡單的界面,在這章,我們將介紹如何使用常用的控件設計實用的界面。

Android中的視圖都是繼承View的。Android的視圖分2種,一種是普通的視圖,一種是ViewGroup。他們的區別在於,ViewGroup里可以放很多普通視圖,我們常見的布局就是一種ViewGroup。

Android中布局是用來排版的,以下是Android中常用的布局列表

名稱

說明

LinearLayout

線性布局

RelativeLayout

相對布局

FrameLayout

幀布局

TableLayout

表格布局

AbsoluteLayout

絕對布局(廢棄)

GridLayout

網格布局(4.0推出的)

 

接下來我們對每種布局進行介紹

 LinearLayout

LinearLayout 我們翻譯為線性布局,他的子元素是水平和垂直方向進行排列,可以通過設置 Orientation 改變排列方式,我們看下效果

首先 新建一個布局文件 linearlayout_demo.xml ,右鍵layout文件夾 選擇 new-Android xml File

在彈出的窗口中輸入 布局文件的名稱

 

Finish后,我們的布局文件就創建好了。

接下來 我們往 linearlayout.xml布局里拖入多個文本控件

目前我拖入了5個文本控件,他是按照從上往下的順序排列的,即垂直排列

 

 

接下來 我們修改 orientation的屬性為horizontal,5個文本控件的排列方式方式了改變,變成了水平方向 的了.接着我們切換到代碼區域

 

android:orientation 是設置LinearLayout排列方向的,也是LinearLayout特有的屬性。

 

  RelativeLayout

RelativeLayout 即相對布局

同樣我們也新建一個 relativelayout_demo.xml 的布局文件,選擇根節點為RelativeLayout

隨意拖幾個控件進入布局

 可以看到 RelativeLayout 和LinearLayout 的區別,RelativeLayout 可以任意擺放控件

RelativeLayout里常用的位置屬性如下:
    android:layout_toLeftOf —— 該組件位於引用組件的左方
    android:layout_toRightOf —— 該組件位於引用組件的右方
    android:layout_above —— 該組件位於引用組件的上方
    android:layout_below —— 該組件位於引用組件的下方
       android:layout_alignParentLeft —— 該組件是否對齊父組件的左端
       android:layout_alignParentRight —— 該組件是否齊其父組件的右端
       android:layout_alignParentTop —— 該組件是否對齊父組件的頂部
       android:layout_alignParentBottom —— 該組件是否對齊父組件的底部
    android:layout_centerInParent —— 該組件是否相對於父組件居中
    android:layout_centerHorizontal —— 該組件是否橫向居中
    android:layout_centerVertical —— 該組件是否垂直居中

  TableLayout

TableLayout顧名思義,此布局為表格布局,適用於N行N列的布局格式。一個TableLayout由許多TableRow組成,一個TableRow就代表TableLayout中的一行。

  TableRow是LinearLayout的子類,它的android:orientation屬性值恆為horizontal,並且它的android:layout_width和android:layout_height屬性值恆為MATCH_PARENT和WRAP_CONTENT。所以它的子元素都是橫向排列,並且寬高一致的。這樣的設計使得每個TableRow里的子元素都相當於表格中的單元格一樣。在TableRow中,單元格可以為空,但是不能跨列。

 

  GridLayout(4.0以上的版本支持,所以如果想直接使用該布局的話,需要設置最低版本號為14)

android4.0以上版本出現的GridLayout布局解決了以上問題。GridLayout布局使用虛細線將布局划分為行、列和單元格,也支持一個控件在行、列上都有交錯排列。而GridLayout使用的其實是跟LinearLayout類似的API,只不過是修改了一下相關的標簽而已,所以對於開發者來說,掌握GridLayout還是很容易的事情。GridLayout的布局策略簡單分為以下三個部分:

  首先它與LinearLayout布局一樣,也分為水平和垂直兩種方式,默認是水平布局,一個控件挨着一個控件從左到右依次排列,但是通過指定android:columnCount設置列數的屬性后,控件會自動換行進行排列。另一方面,對於GridLayout布局中的子控件,默認按照wrap_content的方式設置其顯示,這只需要在GridLayout布局中顯式聲明即可。

       其次,若要指定某控件顯示在固定的行或列,只需設置該子控件的android:layout_row和android:layout_column屬性即可,但是需要注意:android:layout_row=”0”表示從第一行開始,android:layout_column=”0”表示從第一列開始,這與編程語言中一維數組的賦值情況類似。

  最后,如果需要設置某控件跨越多行或多列,只需將該子控件的android:layout_rowSpan或者layout_columnSpan屬性設置為數值,再設置其layout_gravity屬性為fill即可,前一個設置表明該控件跨越的行數或列數,后一個設置表明該控件填滿所跨越的整行或整列。

 

GridLayout 常用的屬性有

android:columnCount 設置列數

android:rowCount 設置行數

android:layout_rowSpan=指定控件占幾行

android:layout_column=指定控件在第幾列

android:layout_row=指定控件在第幾行

比如 我們想做一個小鍵盤

 

tabLeLayout 做不到的,因為他不可以跨列,LinearLayout可以做到 但是需要嵌套很多層,如果使用GridLayout 就可以很簡單的做出來

代碼如下

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="wrap_content"
 5     android:columnCount="4" >
 6 
 7     <Button
 8         android:id="@+btn_1_1"
 9         android:layout_column="1"
10         android:text="print" />
11 
12     <Button
13         android:id="@+btn_1_2"
14         android:text="Scro" />
15 
16     <Button
17         android:id="@+btn_1_3"
18         android:text="Pau" />
19 
20     <Button
21         android:id="@+btn_2_1"
22         android:text="Num" />
23 
24     <Button
25         android:id="@+btn_2_2"
26         android:text="/" />
27 
28     <Button
29         android:id="@+btn_2_3"
30         android:text="*" />
31 
32     <Button
33         android:id="@+btn_2_4"
34         android:text="-" />
35 
36     <Button
37         android:id="@+btn_3_1"
38         android:text="7" />
39 
40     <Button
41         android:id="@+btn_3_2"
42         android:text="8" />
43 
44     <Button
45         android:id="@+btn_3_3"
46         android:text="9" />
47 
48     <Button
49         android:id="@+btn_3_4"
50         android:layout_gravity="fill_vertical"
51         android:layout_rowSpan="2"
52         android:text="+" />
53 
54     <Button
55         android:id="@+btn_4_1"
56         android:text="4" />
57 
58     <Button
59         android:id="@+btn_4_2"
60         android:text="5" />
61 
62     <Button
63         android:id="@+btn_4_3"
64         android:text="6" />
65 
66     <Button
67         android:id="@+btn_5_1"
68         android:text="1" />
69 
70     <Button
71         android:id="@+btn_5_2"
72         android:text="2" />
73 
74     <Button
75         android:id="@+btn_5_3"
76         android:text="3" />
77 
78     <Button
79         android:id="@+btn_5_4"
80         android:layout_gravity="fill_vertical"
81         android:layout_rowSpan="2"
82         android:text="Ent" />
83 
84     <Button
85         android:id="@+btn_6_1"
86         android:layout_columnSpan="2"
87         android:layout_gravity="fill_horizontal"
88         android:text="0" />
89 
90     <Button
91         android:id="@+btn_6_2"
92         android:text="." />
93 
94 </GridLayout>


常用布局 簡單的給大家介紹到這個地方, 接下來我們介紹Android常用的控件

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM