歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android應用開發之LinearLayout(線性布局)

Android應用開發之LinearLayout(線性布局)

日期:2017/3/1 10:49:26   编辑:Linux編程

“ LinearLayout ”翻譯成中文是“線性布局”,所謂線性布局就是在該標簽下的所有子元素會根據其 orientation 屬性的值來決定是按行或者是按列逐個顯示。

示例main.xml布局文件如下:

  1. <?xml version="1.0"encoding ="utf-8"?>
  2. <LinearLayout
  3. xmlns:Android "http://schemas.android.com/apk/res/android"
  4. android:orientation= "vertical"
  5. android:layout_width= "fill_parent"
  6. android:layout_height= "fill_parent"
  7. >
  8. <TextView
  9. android:layout_width= "fill_parent"
  10. android:layout_height= "wrap_content"
  11. android:text ="@string/name_text"
  12. />
  13. < EditText
  14. android:layout_width= "fill_parent"
  15. android:layout_height= "wrap_content" />
  16. < Button
  17. android:layout_width= "wrap_content"
  18. android:layout_height= "wrap_content"
  19. android:text ="@string/cancle_button"
  20. />
  21. < Button
  22. android:layout_width= "wrap_content"
  23. android:layout_height= "wrap_content"
  24. android:text ="@string/ok_button" />
  25. </LinearLayout >

其對應 strings.xml 內容如下:

  1. <?xml version="1.0"encoding = "utf-8" ?>
  2. < resources>
  3. < string name= "hello" > Hello World, UIActivity! </ string >
  4. < string name= "app_name" > 用戶界面 </ string >
  5. < string name= "name_text" > 請輸入用戶名 </ string >
  6. < string name= "ok_button" > 確定 </ string >
  7. < string name= "cancle_button" > 取消 </ string >
  8. </ resources>

運行後,如下圖:


“ xmlns:android ”指定命名空間,頂級元素必須指定命名空間。而在該命名空間中的控件的屬性如 layout_width ,要在屬性前加上“android :”做前綴��

“ layout_width ”指定該元素的寬度,可選值有三種,“ fill_parent ”、“ wrap_content ”、具體數字(單位為 px )。其中“ fill_parent ”代表填滿其父元素,對於頂級元素來說,其父元素就是整個手機屏幕。“ wrap_content ”代表該元素的大小僅包裹其自身內容,而數字則代表其占相應的 px 。

“ layout_height ”指定該元素的高度,可選參數值與“ layout_width ”的參數意義相同。

“ orientation ”指定子元素排列方式,其中指定為“ vertical ”則是子元素垂直排列,每個子元素會占獨立的一行,如上圖,而另一個可選值為“ horizontal ”代表子元素水平排列,即每個子元素會占獨立的一列。示例 main.xml 布局文件如下。其對應的

Copyright © Linux教程網 All Rights Reserved