学习Launcher2和Settings

 


参考文献:

http://zhu-apollo-163-com.iteye.com/blog/1281627--主要讲解launcher2

http://developer.android.com/design/patterns/settings.html#flow-structure---讲解Settings

http://blog.csdn.net/fzh0803/article/details/6279995---学习launcher2

http://www.eoeandroid.com/thread-241472-1-1.html

http://blog.csdn.net/shichaosong/article/details/8174621---总体学习

 在android 4.2基础上,总结Launcher2:

 

1. 什么android的Launcher ?

 

答: 从字面意思来看就是“发射台、启动台、启动物体”的意思,我个人把它理解成Android的“桌面启动器”,就是我们启动手机或者平板电脑后显示在我们眼前的这个界面,相当于Windows的桌面。

 

另外,好像是从android2.1版本开始launcher的版本变成了launcher2。

注:目前android 4.4的已经出现launcher3

2. Launcher和系统层是否有关联?

答:没有关联,launcher就相当于一个应用程序一样。当你修改launcher时,无需全编译,只需要编译这个launcher即可,这里称之为小编译,编译后一起打包就能升级启动。注:我这里把关联理解为是否和底层的C库或者内核关联。

3. Launcher包含哪些?

答: 从界面上来看,launcher2的界面构成如图1所示:

                                                       图1 Launcher2 界面布局示意图

Launcher包含了:workspace、壁纸、左右下角的点(单击这个点可以切换屏幕)、右边的DeleteZone 和 HandleView。

那什么是workspace呢? 当你左右滑动屏幕时,每个屏幕就相当于一个workspace,一般来说我们可以自定义屏幕的数目,平常我们见到的都是5个。

如图1所示,我们可以看到有workspace、workspace中包含的cell(就是那些小方格)。

壁纸:常按屏幕,便会出现wallpaper选择项--Wallpapers/Video Wallpaper/Live Wallpapers等

注意:状态栏statusbar不属于Launcher,状态栏和系统层有关,关于状态栏的修改请点击这里

4. Launcher的源代码在那个目录下?包含哪些文件?

答:主要包含几个java文件和xml文件,当然了,还有图片文件。

因为Launcher好比一个应用程序一样,所以他的目录主要就在packages包下面。

java文件: /packages/apps/Launcher2/src/com/android/launcher2/

xml文件和图片等资源文件:/packages/apps/Launcher2/res/

 

关于java文件的分析,请参考:http://blog.csdn.net/fzh0803/archive/2011/03/26/6279995.aspx

 

关于布局文件请看下面的分析。

5. 核心的布局文件有哪些?

答:总布局文件分别在:

横屏: packages/apps/Launcher2/res/layout-land/launcher.xml

竖屏: packages/apps/Launcher2/res/layout-port/launcher.xml

就用横屏的launcher.xml分析一下,官方代码是:

 

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<!-- Full screen view projects under the status bar and contains the background -->
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher"

    android:id="@+id/launcher"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.android.launcher2.DragLayer
        android:id="@+id/drag_layer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <!-- The workspace contains 5 screens of cells -->
        <com.android.launcher2.Workspace
            android:id="@+id/workspace"
            android:layout_width="match_parent"android:layout_height="match_parent"
            android:paddingLeft="@dimen/workspace_left_padding"
            android:paddingRight="@dimen/workspace_right_padding"
            android:paddingTop="@dimen/workspace_top_padding"
            android:paddingBottom="@dimen/workspace_bottom_padding"
            launcher:defaultScreen="2"
            launcher:cellCountX="@integer/cell_count_x"
            launcher:cellCountY="@integer/cell_count_y"
            launcher:pageSpacing="@dimen/workspace_page_spacing"
            launcher:scrollIndicatorPaddingLeft="@dimen/qsb_bar_height"
            launcher:scrollIndicatorPaddingRight="@dimen/button_bar_height">
            <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.launcher2.Workspace>        
        <include
            android:id="@+id/qsb_divider"
            layout="@layout/workspace_divider"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/qsb_bar_height"
            android:layout_gravity="left" />
    <!-- 
        <include
            android:id="@+id/dock_divider"
            layout="@layout/workspace_divider"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginRight="@dimen/button_bar_height"
            android:layout_gravity="right" />

         -->
        <include
            android:id="@+id/paged_view_indicator"
            layout="@layout/scroll_indicator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom" />       
        <include 
            layout="@layout/hotseat"
            android:id="@+id/hotseat"
            android:layout_width="@dimen/button_bar_height_plus_padding"
            android:layout_height="@dimen/button_bar_width_plus_padding"
            android:layout_gravity="right|center_vertical" />

        <include
            android:id="@+id/qsb_bar"
            layout="@layout/qsb_bar" />
    <!-- The Workspace cling must appear under the AppsCustomizePagedView below to ensure
             that it is still visible during the transition to AllApps and doesn't overlay on
             top of that view. -->
        <include layout="@layout/workspace_cling"
            android:id="@+id/workspace_cling"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone" />

        <include layout="@layout/folder_cling"
            android:id="@+id/folder_cling"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone" />
    <com.android.launcher2.DrawableStateProxyView
            android:id="@+id/voice_button_proxy"
            android:layout_width="@dimen/qsb_bar_height"
            android:layout_height="@dimen/app_icon_size"
            android:layout_gravity="top|left"
            android:layout_marginTop="64dp"
            android:clickable="true"
            android:onClick="onClickVoiceButton"
            android:importantForAccessibility="no"
            launcher:sourceViewId="@+id/voice_button" />

        <include layout="@layout/apps_customize_pane"
            android:id="@+id/apps_customize_pane"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="invisible" />
    </com.android.launcher2.DragLayer>
</FrameLayout>


 

从这个文件可以很容易看出,包含了workspace,qsb(google搜索条),hotseat等。

<com.android.launcher2.workspace/>这一模块正好是5个屏幕的布局,他们都引用了同一个布局文件。

从这一模块中,我们可以看到,怎么定义cell,和每个cell之间的间距大小,如下分析:

            launcher:defaultScreen="2"---代表主workspace是第3屏
            launcher:cellCountX="@integer/cell_count_x"---代表每个workspace的x方向所含的cell个数
            launcher:cellCountY="@integer/cell_count_y"---代表每个workspace的y方向所含的cell个数

更详细的数值研究,这里便不说了。

6.  能否增加或者减少cell的数量?

答:可以。主要在/packages/apps/Launcher2/res/values-{ }/config.xml中修改。

注:{ }中的值,根据各分辨录的情况而定。

eg:

xml定义;

<resources>
    <integer name="cell_count_x">6</integer>
    <integer name="cell_count_y">5</integer>
    <integer name="hotseat_cell_count">5</integer>
    <integer name="hotseat_all_apps_index">2</integer>
.........

1)cell_count_x和cell_count_y表示在x方向上和y方向上,每个workspace所放的cell数量

以上数字可以根据实际情况修改。

以上内容比较简单,只是大概介绍了launcher的基本特性,后续还会加深研究。

 

插入内容:

android中include的使用

在一个项目中我们可能会需要用到相同的布局设计,如果都写在一个xml文件中,代码显得很冗余,并且可读性也很差,所以我们可以把相同布局的代码单独写成一个模块,然后用到的时候可以通过<include /> 标签来重用layout代码。

也就是说,如果在程序中多次用到一部分相同的布局,可以先将这部分布局定义为一个单独的XML,然后在需要的地方通过<include>引入。如下:

main.xml

 1: <?xml version="1.0" encoding="utf-8"?>   
2: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
 3: android:layout_width="wrap_content"  
 4: android:layout_height="wrap_content">   
5: <include android:layout_width="wrap_content"   
 6: android:layout_height="wrap_content"    
7: android:id="@+id/cell1"   
8: layout="@layout/item"    
9: android:layout_marginTop="10dp"   10: android:layout_marginLeft="45dp"/>  
11: <include android:layout_width="wrap_content"   
12: android:layout_height="wrap_content"   
13: android:id="@+id/cell2"   
14: layout="@layout/item"   
15: android:layout_toRightOf="@+id/cell1"  
16: android:layout_alignTop="@+id/cell1"   
17: android:layout_marginLeft="20dp"/>  
18: <include android:layout_width="wrap_content"   
19: android:layout_height="wrap_content"   
20: android:id="@+id/cell3"   
21: layout="@layout/item"   
22: android:layout_toRightOf="@+id/cell2"   
23: android:layout_alignTop="@+id/cell1"   
24: android:layout_marginLeft="20dp"/>  
25: </RelativeLayout>

item.xml

1: <?xml version="1.0" encoding="utf-8"?>   
2: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
3: android:layout_width="wrap_content"    
4: android:layout_height="wrap_content"    
5: android:visibility="invisible">   
6: <ImageView android:background="#000000"    
7: android:id="@+id/iv_img"    
8: android:layout_width="wrap_content"    
9: android:layout_height="wrap_content"   
10: android:clickable="true"   
11: android:focusable="false" />  
12: <TextView android:id="@+id/tv_name"   
13: android:layout_width="wrap_content"   
14: android:layout_height="wrap_content"   
15: android:textColor="#a17006"   
16: android:textStyle="bold"   
17: android:textSize="22dp"   
18: android:layout_alignLeft="@+id/iv_img"   
19: android:layout_below="@+id/iv_img" />  
20: </RelativeLayout><p></p><p><span style="font-size:12px;"></span> </p>

使用include时需要注意的是要指定宽高属性,否则,可能会出现一些意想不到的效果,比如引用了三次,而界面上只显示了一个item

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值