天天看点

android Launcher源码解析01:UI布局详解一

        本系列文章将开始android lancher源码分析,使用的例子是android 2.3中自带的launcher3源码。其下载地址为:http://download.csdn.net/detail/xianming01/4383598

        本文为第一篇文章,介绍一下lancher的UI布局。

1、布局

      运行该APK,则其执行结果为:

android Launcher源码解析01:UI布局详解一

      按home键以后:

android Launcher源码解析01:UI布局详解一

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
android Launcher源码解析01:UI布局详解一
  • DragLayer 有几个child,其中最重要的是WorkPlace 和 AllApps2D如下:
android Launcher源码解析01:UI布局详解一
  • WorkPlace有五个child,每个是一个CellLayout
android Launcher源码解析01:UI布局详解一

每个CellLayout里面放一些放widget和应用快捷方式(BubbleTextView)

android Launcher源码解析01:UI布局详解一
  • AllApps2D 展示所有应用列表
android Launcher源码解析01:UI布局详解一

参考资料: 4 . launcher 的UI布局详解