1. 程式人生 > >Android 4.0 Launcher2源碼分析——主布局文件(轉)

Android 4.0 Launcher2源碼分析——主布局文件(轉)

not sch png 默認 顯示效果 target ots eat col

本文來自http://blog.csdn.net/chenshaoyang0011

Android系統的一大特色是它擁有的桌面通知系統,不同於IOS的桌面管理,Android有一個桌面系統用於管理和展示APP以及桌面Widget等。

Android提供一個默認的桌面應用,當然我們也可以使用第三方的桌面應用。Android的Launcher的源碼在 \packages\apps\Launcher2。

Launcher2的主布局文件包括res\layout-land\launcher.xml

, res\layout-port\launcher.xml , res\layout-sw600dp\launcher.xml ,分別適配橫屏,豎屏和7寸平板。內容大同小異,這裏就以res\layout-port\launcher.xml的為例。

首先讓我們看看launcher.xml中的結構:

  1. <com.android.launcher2.DragLayer
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher"
  4. android:id="@+id/drag_layer"
  5. ...
  6. >
  7. <!-- Keep these behind the workspace so that they are not visible when
  8. we go into AllApps -->
  9. <include
  10. android:id="@+id/dock_divider"
  11. layout="@layout/workspace_divider"
  12. ...
  13. />
  14. <!-- 分頁指示器 -->
  15. <include
  16. android:id="@+id/paged_view_indicator"
  17. layout="@layout/scroll_indicator"
  18. ...
  19. />
  20. <!-- The workspace contains 5 screens of cells -->
  21. <com.android.launcher2.Workspace
  22. android:id="@+id/workspace"
  23. ...
  24. >
  25. <!-- 五個分屏,默認顯示cell3 -->
  26. <include android:id="@+id/cell1" layout="@layout/workspace_screen" />
  27. <include android:id="@+id/cell2" layout="@layout/workspace_screen" />
  28. <include android:id="@+id/cell3" layout="@layout/workspace_screen" />
  29. <include android:id="@+id/cell4" layout="@layout/workspace_screen" />
  30. <include android:id="@+id/cell5" layout="@layout/workspace_screen" />
  31. </com.android.launcher2.Workspace>
  32. <!-- 搜索框/刪除框 -->
  33. <include
  34. android:id="@+id/qsb_bar"
  35. layout="@layout/qsb_bar" />
  36. <!-- 顯示具體全部應用的界面,包括APPS、WIGHETS的tab標簽,以及顯示ALL APP的頁面和現實APP WIGHETS的頁面 -->
  37. <include layout="@layout/apps_customize_pane"
  38. android:id="@+id/apps_customize_pane"
  39. ..
  40. />
  41. <!-- WorkSpace最下面的五個快捷位置 -->
  42. <include layout="@layout/hotseat"
  43. android:id="@+id/hotseat"
  44. ..
  45. />
  46. <!-- 剛啟動的時候顯示的指導頁 -->
  47. <include layout="@layout/workspace_cling"
  48. android:id="@+id/workspace_cling"
  49. ...
  50. />
  51. <!-- 是第一次進入全部應用之後顯示的指導頁 -->
  52. <include layout="@layout/folder_cling"
  53. android:id="@+id/folder_cling"
  54. ...
  55. />
  56. </com.android.launcher2.DragLayer>

接著我們來一一認識每一個View控件。

1、最外層的DragLayer,是一個繼承自FramLayout的View控件,顯示的就是整個桌面根容器。桌面的所有控件都是位於DragLayer中。

2、id/dock_divider,使用了布局workspace_divider,其實就是一個ImageView。是Workspace與Hotseat之間的分割線。

技術分享

3、id/paged_view_indicator,使用了布局scroll_indicator,顯示效果是在id/dock_divider上顯示一條淡藍色的橫線,來指示當分屏所處的位置

4、id/workspace ,工作空間擁有五個workspace_screen,即有五個分屏,每個分屏都可以放置shortcut和AppWidget,效果如下:

技術分享

5、id/cell1..cell5 ,分別代表五個分屏

6、id/qsb_bar 搜索框/刪除框,根據需要進行切換

技術分享 技術分享

7、id/apps_customize_pane,效果如下

技術分享

8、id/hotseat 即主屏幕下方的五個快捷位置

技術分享

9、id/workspace_cling當第一次運行Launcher2時,會顯示的用於指導的動畫,以後不再顯示

技術分享技術分享

10、id/folder_cling,第一次使用Folder時,展示給用戶的指導畫面。

技術分享

這樣,我們已經可以使每個UI界面及組件都對號入座,這會使接下來分析的時候更加清晰。

Android 4.0 Launcher2源碼分析——主布局文件(轉)