android Launcher源码解析02:Hotseat

本文深入探讨了Android Launcher3中的Hotseat实现,分析了Hotseat的界面设计和布局文件,以及如何实现Hotseat按钮的状态变化。通过源码解析,展示了如何将Hotseat的按钮数量从3个扩展到5个,并提供了优化步骤和相关文件的修改方法。
摘要由CSDN通过智能技术生成

        本文主要介绍launcher3中Hotseat的实现,同时参照go launcher的界面,给出了一个demo.

1、 hotseat的界面

在launcher3中,其主界面如下图所示,其中用红色圈标注了其中的hotseat(如下图左):

           

       本文给出了一个改装版本,图上图右所示。这样是目前市面上大多数launcher产品实现的方案。当然,部分产品,还将这5个图标中的最右边一个设置为可以自动更改,比如说,你可以将右边那个浏览器图标改为任意app图标。

2、launcher3中hotseat的实现

       我们来看一下他们的layout文件:

res/layout-port/launcher.xml:

<RelativeLayout
        android:id="@+id/all_apps_button_cluster"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/button_bar_height"
        android:layout_gravity="bottom|center_horizontal"
        android:paddingTop="2dip"
        >

        <com.android.launcher3.HandleView
            style="@style/HotseatButton"
            android:id="@+id/all_apps_button"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"

            android:src="@drawable/all_apps_button"
            launcher:direction="horizontal"
            />

        <ImageView
            android:id="@+id/hotseat_left"
            style="@style/HotseatButton.Left"
            android:layout_toLeftOf="@id/all_apps_button"

            android:src="@drawable/hotseat_phone"

            android:onClick="launchHotSeat"
            />

        <ImageView
            android:id="@+id/hotseat_right"
            style="@style/HotseatButton.Right"
            android:layout_toRightOf="@id/all_apps_button"

            android:src="@drawable/hotseat_browser"

            android:onClick="launchHotSeat"
            />

    </RelativeLayout>
        其中,你可以看到,一个打电话图标和一个浏览器图标,一个app list的图标。

2.1 点击时的状态变化

      关于这一点,你在drawable文件下,可以看到hotseat_phone.xml,all_apps_button.xml,hotseat_browser.xml这三个按钮的状态信息,其中包括焦点在按钮上、点击等状态,以hotseat_phone.xml内容为例:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/hotseat_phone_pressed" />
    <item android:state_focused="true" android:state_window_focused="true" android:drawable="@drawable/hotseat_phone_focused" />
    <item android:state_focused="true" android:state_window_focused="false" android:drawable="@drawable/hotseat_phone_normal" />
    <item android:drawable="@drawable/hotseat_phone_normal" />
</selector>
2.2 按钮背景及位置

       关于这一点,可以看到hotseat_phone位于app list的左边,其中由如下属性定义:

android:layout_toLeftOf="@id/all_apps_button"
        同时在style文件中,还定义其背景以及间隔信息:

<style name="HotseatButton">
        <item name="android:paddingLeft">12dip</item>
        <item name="android:paddingRight">12dip</item>
        <item name="android:background">@drawable/hotseat_bg_center</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">fill_parent</item>
        <item name="android:scaleType">center</item>
        <item name="android:focusable">true</item>
        <item name="android:clickable">true</item>
    </style>
    <style name="HotseatButton.Left">
        <item name="android:layout_marginLeft">4dip</item>
        <item name="android:background">@drawable/hotseat_bg_left</item>
    </style>
    <style name="HotseatButton.Right">
        <item name="android:layout_marginRight">4dip</item>
        <item name="android:background">@drawable/hotseat_bg_right</item>
    </style>
2.3 初始化

      在launcher.java文件中,对其进行初始化,主要包括三个函数:

setupViews();//初始化各个view

loadHotseats();//初始化打电话和搜索那两个按钮的intent

launchHotSeat(View v)//实际上就是点击了hotseat之后,转入对应的程序而已

其中代码分析如下:

setupViews();//初始化各

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值