Android Launcher分析和修改10——HotSeat深入进阶

前面已经写过Hotseat分析的文章,主要是讲解如何在Launcher里面配置以及修改Hotseat的参数。今天主要是讲解一下如何在Hotseat里面的Item显示名称。这个小问题昨天折腾了半天,最后幸亏我亲爱的女朋友大人提醒了我,才想到原因。在此十分感谢我女朋友大人的提醒,幸好她不是做程序员,不然我就要失业了O(∩_∩)O哈哈~

默认Hotseat里面的元素都没有标题

24175052-c4f27b7abc0a47e8a1861237000f0ee9.jpg 

1、Hotseat隐藏文件夹标题

  刚开始想解决这个问题的想法是找到按钮对象生成的地方,修改一下就好了。因为我上次分析Hotseat的时候,记得有一个地方把Hotseat的按钮文字屏蔽了,我想只要打开就好。

  1. //Edited by mythou
  2. //http://www.cnblogs.com/mythou/
  3.     if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) 
  4. {
  5. if(OWLLauncherDebug.HotSeat) Log.d(mythou, "addInScreen------->CONTAINER_HOTSEAT");

  6. layout = mLauncher.getHotseat().getLayout();
  7. child.setOnKeyListener(null);

  8. //隐藏Hotseat里面文件夹的名称
  9. if (child instanceof FolderIcon) 
  10. {
  11. ((FolderIcon) child).setTextVisible(false);
  12. }

  13.        //.........
  14.      }
复制代码

  上面这段代码是在Workspace里面addInScreen()的部分代码,是绑定workspace元素的时候调用的方法,里面的确有一个判断是否隐藏文件夹的名称,但是没看到隐藏普通快捷方式的代码。我先把文件夹标题隐藏的功能去掉,但是hotseat里面的文件夹还是没有名称。没办法,只能从头看看Hotseat上面那些快捷方式如何添加和加载。

2、Hotseat上Item如何生成

  Hotseat上面的快捷方式可以分为两种,一种是普通快捷方式,可以在default_workspace里面配置。另外一个是全部应用列表的按钮,这个实在Hotseat.java里面动态生成的。我们先看看普通的快捷方式如何生成,一般的快捷方式,通过加载default_workspace,这个过程跟workspace上面的按钮一样,前面已经有详细分析,这里不多说。我们主要看看如何生成快捷方式的对象,其实这个跟workspace也是一样,我们看看下面代码:

  1. //Edited by mythou
  2. //http://www.cnblogs.com/mythou/
  3.   public void bindItems(ArrayList shortcuts, int start, int end) 
  4. {
  5.      //.........for (int i=start; i<END; 
  6. {
  7.        //.........if(OWLLauncherDebug.HotSeat) Log.d("OWL_Launcher", "bindItems------->item="+item);

  8. switch (item.itemType) 
  9. {
  10. case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
  11. case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
  12.            //生成Item的View对象
  13. View shortcut = createShortcut((ShortcutInfo)item);
  14.            //添加到对应的screen里面
  15. workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
  16. item.cellY, 1, 1, false);
  17. break;
  18. }
  19. }
  20. }
复制代码

  上面是Launcher里面的bindItems()方法的部分代码,bindItems()这个方法,看过我前面分析Launcher数据加载的朋友应该不会陌生,这是绑定Launcher数据时候Launcher回调的一个方法,Hotseat的Item对象就在这里生成。调用了createShortcut()方法,下面我们看看createShortcut()是如何生成Item对象。

  1. //Edited by mythou
  2. //http://www.cnblogs.com/mythou/
  3.   View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) 
  4. {
  5. if(OWLLauncherDebug.HotSeat) Log.d("OWL_Lancher", "addInScreen------->info="+info);
  6. //生成Item的快捷方式,Hotseat里面的按钮也是BubbleTextView
  7. BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
  8. favorite.applyFromShortcutInfo(info, mIconCache);
  9. favorite.setOnClickListener(this);
  10. return favorite;
  11. }
复制代码

  从这里可以看出,其实Hotseat里面的按钮也是BubbleTextView的对象,也就是跟一般的workspace里面的按钮一样。对象生成以后会添加到对应的Screen里面。前面Hotseat的文章我也说过,Hotseat其实也是一个CellLayout的screen。在程序里面是标记为最后一个screen。

3、Item添加到Hotseat

  1. //Edited by mythou
  2. //http://www.cnblogs.com/mythou/
  3.   void addInScreen(View child, long container, int screen, int x, int y, int spanX, int spanY,
  4. boolean insert) 
  5. {
  6. if(OWLLauncherDebug.HotSeat) Log.d(OWL, "addInScreen------->screen="+screen);
  7. final CellLayout layout;
  8.      //针对Hotseat的对象做特殊处理 mythou
  9. if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) 
  10. {
  11. if(OWLLauncherDebug.HotSeat) Log.d(OWL, "addInScreen------->CONTAINER_HOTSEAT");

  12. layout = mLauncher.getHotseat().getLayout();
  13. child.setOnKeyListener(null);

  14. //隐藏Hotseat文件夹标题
  15. if (child instanceof FolderIcon) 
  16. {
  17. // ((FolderIcon) child).setTextVisible(false);
  18. }if(OWLLauncherDebug.HotSeat) Log.d(OWL, "addInScreen------->CONTAINER_HOTSEAT screen="+screen);

  19. if (screen < 0) 
  20. {
  21. screen = mLauncher.getHotseat().getOrderInHotseat(x, y);

  22. else 
  23. {//获取child的位置,返回true添加成功,false失败 这里其实是Hotseat里面Item的位置
  24. x = mLauncher.getHotseat().getCellXFromOrder(screen);
  25. y = mLauncher.getHotseat().getCellYFromOrder(screen);
  26. }

  27. if(OWLLauncherDebug.HotSeat) Log.d(OWL, "addInScreen-------> x="+x + " y="+y);


  28. }
复制代码

  上面就是把Hotseat里面元素添加到Hotseat对应的CellLayout里面去,里面有一部分针对Hotseat元素的特殊处理,也就是刚开始说的隐藏文件夹标题以及技术元素位置的代码。除了这些,我并没有找到隐藏按钮文字的方法。后来只能看看Hotseat里面另外一个按钮(AllAPP)能不能显示文字。

4、Hotseat的AllAPP按钮

  1. //Edited by mythou
  2. //http://www.cnblogs.com/mythou/
  3.   void resetLayout() 
  4. {
  5.      //清空Hotseat里面所有的元素
  6. mContent.removeAllViewsInLayout();

  7. //添加ALLAPP按钮
  8. Context context = getContext();
  9. LayoutInflater inflater = LayoutInflater.from(context);
  10. //修改为自己定义的button配置application, mythou
  11. BubbleTextView allAppsButton = (BubbleTextView)
  12. inflater.inflate(R.layout.application, mContent, false);
  13.      //设置allapp按钮的图片
  14. allAppsButton.setCompoundDrawablesWithIntrinsicBounds(null,
  15. context.getResources().getDrawable(R.drawable.all_apps_button_icon), null, null);
  16. allAppsButton.setText(context.getString(R.string.all_apps_button_label));
  17. allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));

  18. int width = allAppsButton.getWidth();
  19. int heigh = allAppsButton.getHeight();
  20. Log.d("OWL_HOTSEAT", "allAppsButton width="+width+" heigh="+heigh);
  21.       
  22.      //.........
  23.   }
复制代码

  上面是Hotseat.java里面的resetLayout()方法,前面也说了Hotseat里面的按钮分为两种,其中AllAPP按钮是代码里面生成的。最后也是调用addViewToCellLayout()方法,addViewToCellLayout()是CellLayout里面的方法,跟前面一样,会添加到CellLayout里面。

5、Hotseat显示Item标题

  前面说了半天都没说到如何修改显示标题。其实这个说起来很简单,Hotseat里面的元素,每个都保存了Item的标题,我还特意把数据打印出来看了,因为刚开始以为系统直接把标题数据删除了。之所以没有显示出来是因为Hotseat设置的高度太低,导致标题部分显示被屏蔽了,很实用也很囧的方法。另外可能会有人要问,既然从显示空间上限制了,为何还要加个判断文件夹标题隐藏?这个问题答案,只要你看看文件夹高度就知道了,自己实践一下才能印象深刻。

  我对Launcher的分析也有一段时间了,不过现在看来对这个程序还不了解,后面还是多花点时间,达到能随心所欲地修改才行!最后谢谢女朋友大人给我提醒,让我省了不少时间,O(∩_∩)O哈!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值