歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發之layout布局+實例

Android開發之layout布局+實例

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

在Android 應用中,用戶界面是非常重要的,它是人與手機之間傳遞、交換信息的媒介和對話接口,是Android 系統的重要組成部分。它實現信息的內部形式與用戶可以接受形式之間的轉換。iPhone 之所以被人們所推崇,除了其功能強大之外,最重要的是完美的UI(用戶界面)設計,在Android 系統中,我們也可以開發出與iPhone

同樣絢麗多彩的UI。

一個Android 應用的用戶界面是由View 和ViewGroup 對象構建的。它們有很多的種類,並且都是View 類的子類,View 類是Android 系統平台上用戶界面表示的基本單元。View類的一些子類被統稱為“widgets(工具)”,它們提供了諸如文本輸入框和按鈕之類的UI

對象的完整實現。ViewGroup 是View 的一個擴展,它可以容納多個子View。通過擴展ViewGroup 類,你可以創建由相互聯系的子View 組成的復合控件。ViewGroup 類同樣可以被擴展用作layout(布局)管理器,如LinearLayout(線性布局)、TableLayout(表格布局)

以及RelativeLayout(相對布局)等布局架構。並且用戶可以通過用戶界面與程序進行交互。

首先我們來說說LinearLayout。

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

線性布局我們一般不會單用的,因為它太局限性了,它只能制作簡單的界面,如果我們想做如下界面,那麼就必須運用嵌套了。

實現代碼如下

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <LinearLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:orientation="horizontal" >
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="@string/username" />
  14. <EditText
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content" />
  17. </LinearLayout>
  18. <LinearLayout
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:orientation="horizontal" >
  22. <TextView
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:text="@string/userpass" />
  26. <EditText
  27. android:layout_width="fill_parent"
  28. android:layout_height="wrap_content" />
  29. </LinearLayout>
  30. <TableLayout
  31. android:layout_width="match_parent"
  32. android:layout_height="match_parent"
  33. android:stretchColumns="*" >
  34. <TableRow >
  35. <Button
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:text="@string/login" />
  39. <Button
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:text="@string/cancel" />
  43. </TableRow>
  44. </TableLayout>
  45. </LinearLayout>
Copyright © Linux教程網 All Rights Reserved