本系列文章將開始android lancher源碼分析,使用的例子是android 2.3中內建的launcher3源碼。其為:http://download.csdn.net/detail/xianming01/4383598
本文為第一篇文章,介紹一下lancher的UI布局。
1、布局
運行該APK,則其執行結果為:
按home鍵以後:
2、布局檔案
我們來看一下,lancher的布局檔案launcher.xml,其源碼為:
<com.android.launcher3.DragLayer xmlns:android="http://schemas.android.com/apk/res/android" xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher3" android:id="@+id/drag_layer" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/all_apps" /> <!-- The workspace contains 3 screens of cells --> <com.android.launcher3.Workspace android:id="@+id/workspace" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="horizontal" android:fadeScrollbars="true" launcher:defaultScreen="2"> <include android:id="@+id/cell1" layout="@layout/workspace_screen" /> <include android:id="@+id/cell2" layout="@layout/workspace_screen" /> <include android:id="@+id/cell3" layout="@layout/workspace_screen" /> <include android:id="@+id/cell4" layout="@layout/workspace_screen" /> <include android:id="@+id/cell5" layout="@layout/workspace_screen" /> </com.android.launcher3.Workspace> <com.android.launcher3.ClippedImageView android:id="@+id/previous_screen" android:layout_width="93dip" android:layout_height="@dimen/button_bar_height" android:layout_gravity="bottom|left" android:layout_marginLeft="6dip" android:scaleType="center" android:src="@drawable/home_arrows_left" android:onClick="previousScreen" launcher:ignoreZone="56dip" android:focusable="true" android:clickable="true" /> <com.android.launcher3.ClippedImageView android:id="@+id/next_screen" android:layout_width="93dip" android:layout_height="@dimen/button_bar_height" android:layout_gravity="bottom|right" android:layout_marginRight="6dip" android:scaleType="center" android:src="@drawable/home_arrows_right" android:onClick="nextScreen" launcher:ignoreZone="-56dip" android:focusable="true" android:clickable="true" /> <com.android.launcher3.DeleteZone android:id="@+id/delete_zone" android:layout_width="@dimen/delete_zone_size" android:layout_height="@dimen/delete_zone_size" android:paddingLeft="@dimen/delete_zone_padding" android:layout_marginBottom="@dimen/half_status_bar_height" android:layout_gravity="right|center_vertical" android:scaleType="center" android:src="@drawable/delete_zone_selector" android:visibility="invisible" launcher:direction="vertical" /> <RelativeLayout android:id="@+id/all_apps_button_cluster" android:layout_height="fill_parent" android:layout_width="@dimen/button_bar_height_portrait" android:layout_gravity="right|center_vertical" android:layout_marginBottom="@dimen/half_status_bar_height" > <com.android.launcher3.HandleView style="@style/HotseatButton" android:id="@+id/all_apps_button" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:src="@drawable/all_apps_button" launcher:direction="vertical" /> <ImageView android:id="@+id/hotseat_left" style="@style/HotseatButton.Left" android:layout_below="@id/all_apps_button" android:src="@drawable/hotseat_phone" android:onClick="launchHotSeat" /> <ImageView android:id="@+id/hotseat_right" style="@style/HotseatButton.Right" android:layout_above="@id/all_apps_button" android:src="@drawable/hotseat_browser" android:onClick="launchHotSeat" /> </RelativeLayout></com.android.launcher3.DragLayer>
3、布局檔案分析
針對其中的各個部分,其圖形為:
- launcher的第一層次UI 是DragLayer
- DragLayer 有幾個child,其中最重要的是WorkPlace 和 AllApps2D如下:
- WorkPlace有五個child,每個是一個CellLayout
每個CellLayout裡面放一些放widget和應用捷徑(BubbleTextView)
參考資料:4 . launcher 的UI布局詳解