.Net碼農學Android---五分鐘瞭解佈局

在android中應用的界面是以xml來組織的,這一點和WPF類似,經過配置xml文件咱們能夠靈活的構建出你本身想要的界面。css

而在全部的xml界面文件中,根節點必須是佈局,即先有佈局,而後在佈局中組織控件或嵌套佈局,android中的佈局有5種,熟悉各類佈局的使用對咱們之後開發中更好的組織界面大有益處,如下簡單介紹。android

  • TableLayout

表格佈局,就是相似咱們在網頁中以表格來組織控件的佈局。以N行N列的形式排列出相應的控件。佈局

 1  <TableLayout  2         android:layout_width="wrap_content"
 3  android:layout_height="wrap_content"
 4  android:layout_alignLeft="@+id/textView1"
 5  android:layout_centerHorizontal="true"
 6  android:layout_centerVertical="true" >
 7 
 8         <TableRow  9             android:id="@+id/tableRow1"
10  android:layout_width="wrap_content"
11  android:layout_height="wrap_content" >
12 
13             <Button 14                 android:layout_width="wrap_content"
15  android:layout_height="wrap_content"
16  android:text="1行1列" />
17 
18             <Button 19                 android:layout_width="wrap_content"
20  android:layout_height="wrap_content"
21  android:text="1行2列" />
22         </TableRow>
23 
24         <TableRow 25             android:id="@+id/tableRow2"
26  android:layout_width="wrap_content"
27  android:layout_height="wrap_content" >
28 
29             <Button 30                 android:layout_width="wrap_content"
31  android:layout_height="wrap_content"
32  android:text="2行1列" />
33 
34             <Button 35                 android:layout_width="wrap_content"
36  android:layout_height="wrap_content"
37  android:text="2行2列" />
38         </TableRow>
39 
40         <TableRow 41             android:id="@+id/tableRow3"
42  android:layout_width="wrap_content"
43  android:layout_height="wrap_content" >
44 
45             <Button 46                 android:layout_width="wrap_content"
47  android:layout_height="wrap_content"
48  android:text="3行1列" />
49 
50             <Button 51                 android:layout_width="wrap_content"
52  android:layout_height="wrap_content"
53  android:text="3行2列" />
54         </TableRow>
55     </TableLayout>

  • LineLayout

線性佈局,比較簡單,就是以水平或垂直的方式一行一個或一列一個的形式擺放控件。學習

 1   <LinearLayout  2         android:layout_width="fill_parent"
 3  android:layout_height="fill_parent"
 4  android:orientation="vertical" >//水平或垂直
 5 
 6         <Button  7             android:layout_width="fill_parent"
 8  android:layout_height="wrap_content"
 9  android:text="1行" />
10 
11         <Button 12             android:layout_width="fill_parent"
13  android:layout_height="wrap_content"
14  android:text="2行" />
15 
16         <Button 17             android:layout_width="fill_parent"
18  android:layout_height="wrap_content"
19  android:text="3行" />
20     </LinearLayout>

  • RelativeLayout

相對佈局,最爲靈活得分一種佈局,用於組織一些複雜界面,在此佈局中的子元素裏與位置相關的屬性將生效。例如android:layout_below,  android:layout_above, android:layout_centerVertical等。注意在指定位置關係時,引用的ID必須在引用以前,先被定義,不然將出現異常。spa

 1  <RelativeLayout  2         android:layout_width="fill_parent"
 3  android:layout_height="fill_parent"
 4  android:orientation="vertical" >
 5 
 6         <Button  7             android:id="@+id/text_01"
 8  android:layout_width="50dp"
 9  android:layout_height="50dp"
10  android:layout_alignParentBottom="true"
11  android:gravity="center"
12  android:text="1" />
13 
14         <Button 15             android:id="@+id/text_02"
16  android:layout_width="50dp"
17  android:layout_height="50dp"
18  android:layout_above="@id/text_01"//相對佈局中的特有屬性,xx之上/之下/之左/之右等
19  android:layout_centerHorizontal="true"
20  android:gravity="center"
21  android:text="2" />
22 
23         <Button 24             android:id="@+id/text_03"
25  android:layout_width="50dp"
26  android:layout_height="50dp"
27  android:layout_above="@id/text_01"
28  android:layout_toLeftOf="@id/text_02"//相對佈局中的特有屬性,在xx的左邊
29  android:gravity="center"
30  android:text="3" />
31     </RelativeLayout>

  • AbsoluteLayout

絕對佈局,在此佈局中的子元素的android:layout_x和android:layout_y屬性將生效,用於描述該子元素的座標位置,一經設置變不能改變位置,適用一些不常常變化的控件或界面。code

 1 <AbsoluteLayout  2         android:layout_width="fill_parent"
 3  android:layout_height="fill_parent"
 4  android:orientation="vertical" >
 5 
 6         <Button  7             android:layout_width="50dp"
 8  android:layout_height="50dp"
 9  android:layout_x="50dp"
10  android:layout_y="50dp"
11  android:background="#999999"
12  android:gravity="center"
13  android:text="1" />
14 
15         <Button 16             android:layout_width="50dp"
17  android:layout_height="50dp"
18  android:layout_x="90dp"
19  android:layout_y="90dp"
20  android:background="#ff654321"
21  android:gravity="center"
22  android:text="2" />
23 
24         <Button 25             android:layout_width="50dp"
26  android:layout_height="50dp"
27  android:layout_x="125dp"
28  android:layout_y="125dp"
29  android:background="#fffedcba"
30  android:gravity="center"
31  android:text="3" />
32     </AbsoluteLayout>

  • FrameLayout

 幀佈局,網上說的有些彆扭,我本身理解就相似css中的z-index,能夠實現遮罩,即有層的效果。注意:全部的幀默認都是從屏幕左上角開始繪製,而後按照控件的聲明順序,依次疊加,層級逐漸升高。 xml

 1 <FrameLayout  2         android:layout_width="fill_parent"
 3  android:layout_height="fill_parent"
 4  android:orientation="vertical" >
 5 
 6         <TextView  7             android:layout_width="fill_parent"
 8  android:layout_height="fill_parent"
 9  android:background="#999999"
10  android:gravity="center"
11  android:text="1" />
12 
13         <TextView 14             android:layout_width="200dp"
15  android:layout_height="100dp"
16  android:background="#ff654321"
17  android:gravity="center"
18  android:text="2" />
19 
20         <TextView 21             android:layout_width="50dp"
22  android:layout_height="50dp"
23  android:background="#fffedcba"
24  android:gravity="center"
25  android:text="3" />
26     </FrameLayout>

五大布局已經介紹完畢,有些簡單,(由於這個佈局我以爲確實也沒什麼好說的,結合配圖我以爲已經很能說明問題了)。我在寫這個系列以前也說了「我並不想作重複性的工做」,相似這種基礎的東西,網上不少人寫的比我好也很詳細,我只是想把本身在學習過程當中認爲有必要和你們分享的一些經驗和技巧寫出來,因此你能夠看到我並無寫什麼HelloWorld之類的,由於與其把時間花在這種已經氾濫的內容上,倒不如多花時間去貢獻一些獨有的,或被你們忽略的內容上。blog

寫技術博客真的很費時間,得理順思路、組織語言、寫Demo、截圖、排版等等,有時候真羨慕那些能夠一週一篇甚至幾天一篇得大牛們。最近學的內容和點都比較多,看着大篇凌亂的筆記,一時間都不知道該從哪去寫,本身更新的有些慢,不過本身仍是會堅持的,一塊兒加油!開發

相關文章
相關標籤/搜索