Android使用者介面設計:線性布局(1)

來源:互聯網
上載者:User

理解布局對於良好的Android程式設計來說很重要。在這個教程中,你將學習到所有關於線性布局的東西,它在螢幕上垂直地或水平地組織使用者介面控制項或者小工具。使用得當,線性布局可以作為基本的布局,基於這個布局來可以設計出許多有趣的Android程式使用者介面。

什麼是線性布局

線性布局是最簡單,Android開發人員使用得最多的布局類型之一,開發人員用它來組織你們的使用者介面上的控制項。線性布局的作用就像它的名字一樣:它將控制群組織在一個垂直或水平的形式。當布局方向設定為垂直時,它裡面的所有子控制項被組織在同一列中;當布局方向設定為水平時,所有子控制項被組織在一行中。

線性布局可以在XML布局資源檔中定義,也可以用Java代碼在程式中動態定義。

展示了一個包含7個TextView控制項的線性布局。這個線性布局方向被設定為垂直,導致每個TextView控制項被顯示在一列當中。每一個TextView控制項的文字屬性都是一個顏色值,背景色就是這個顏色;通過將控制項的layout_width屬性設定為fill_parent,每個控制項都展開到螢幕寬度。

 

用XML布局資源定義線性布局

設計程式使用者介面最方便和可維護的方法是建立XML布局資源。這個方法極大地簡化了UI設計過程,它將很多靜態建立和使用者介面控制項的布局以及控制項屬性的定義移到了XML中,而不是寫代碼。

XML布局資源必須被儲存在項目目錄的/res/layout下。讓我們看看前一節介紹的彩虹線性布局。這個螢幕基本上就是一個設定為鋪滿整個螢幕的垂直線性布局,這通過設定它的layout_width和layout_height屬性為fill_parent來實現。適當地將其命名為/res/layout/rainbow.xml,XML定義如下:

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2.    
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  4.    
  5. android:layout_width="fill_parent" android:layout_height="fill_parent" 
  6.    
  7. android:orientation="vertical"> 
  8.    
  9. <TextView android:text="RED" android:id="@+id/TextView01" 
  10.    
  11. android:layout_height="wrap_content" android:background="#f00" 
  12.    
  13. android:layout_width="fill_parent" android:layout_weight=".14" 
  14.    
  15. android:gravity="center" android:textColor="#000"></TextView> 
  16.    
  17. <TextView android:text="ORANGE" android:id="@+id/TextView02" 
  18.    
  19. android:layout_height="wrap_content" android:layout_width="fill_parent" 
  20.    
  21. android:layout_weight=".15" android:background="#ffa500" 
  22.    
  23. android:gravity="center" android:textColor="#000"></TextView> 
  24.    
  25. <TextView android:text="YELLOW" android:id="@+id/TextView03" 
  26.    
  27. android:layout_height="wrap_content" android:layout_width="fill_parent" 
  28.    
  29. android:layout_weight=".14" android:background="#ffff00" 
  30.    
  31. android:gravity="center" android:textColor="#000"></TextView> 
  32.    
  33. <TextView android:text="GREEN" android:id="@+id/TextView04" 
  34.    
  35. android:layout_height="wrap_content" android:layout_width="fill_parent" 
  36.    
  37. android:layout_weight=".15" android:background="#0f0" android:gravity="center" 
  38.    
  39. android:textColor="#000"></TextView> 
  40.    
  41. <TextView android:text="BLUE" android:id="@+id/TextView05" 
  42.    
  43. android:layout_height="wrap_content" android:layout_width="fill_parent" 
  44.    
  45. android:layout_weight=".14" android:background="#00f" android:gravity="center" 
  46.    
  47. android:textColor="#fff"></TextView> 
  48.    
  49. <TextView android:text="INDIGO" android:id="@+id/TextView06" 
  50.    
  51. android:layout_height="wrap_content" android:layout_width="fill_parent" 
  52.    
  53. android:layout_weight=".14" android:background="#4b0082" 
  54.    
  55. android:gravity="center" android:textColor="#fff"></TextView> 
  56.    
  57. <TextView android:text="VIOLET" android:id="@+id/TextView07" 
  58.    
  59. android:layout_height="wrap_content" android:layout_width="fill_parent" 
  60.    
  61. android:layout_weight=".14" android:background="#ee82ee" 
  62.    
  63. android:gravity="center" android:textColor="#000"></TextView> 
  64.    
  65. </LinearLayout> 

可能你會注意到這個線性布局的每一個子控制項都有很多有趣的屬性,包括一個叫做layout_weight的屬性。一會我們會講到

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.