Android Launcher分析和修改10——HotSeat深入進階


  前面已經寫過Hotseat分析的文章,主要是講解如何在Launcher里面配置以及修改Hotseat的參數。今天主要是講解一下如何在Hotseat里面的Item顯示名稱。這個小問題昨天折騰了半天,最后幸虧我親愛的女朋友大人提醒了我,才想到原因。在此十分感謝我女朋友大人的提醒,幸好她不是做程序員,不然我就要失業了O(∩_∩)O哈哈~

(PS:新建的QQ群,有興趣可以加入一起討論:Android群:322599434)

 

默認Hotseat里面的元素都沒有標題

 

 

1、Hotseat隱藏文件夾標題

  剛開始想解決這個問題的想法是找到按鈕對象生成的地方,修改一下就好了。因為我上次分析Hotseat的時候,記得有一個地方把Hotseat的按鈕文字屏蔽了,我想只要打開就好。

//Edited by mythou
//http://www.cnblogs.com/mythou/
    if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) 
        {
            if(OWLLauncherDebug.HotSeat) Log.d(mythou, "addInScreen------->CONTAINER_HOTSEAT");
            
            layout = mLauncher.getHotseat().getLayout();
            child.setOnKeyListener(null);

            //隱藏Hotseat里面文件夾的名稱
            if (child instanceof FolderIcon) 
            {
                ((FolderIcon) child).setTextVisible(false);
            }

       //.........      }

  上面這段代碼是在Workspace里面addInScreen()的部分代碼,是綁定workspace元素的時候調用的方法,里面的確有一個判斷是否隱藏文件夾的名稱,但是沒看到隱藏普通快捷方式的代碼。我先把文件夾標題隱藏的功能去掉,但是hotseat里面的文件夾還是沒有名稱。沒辦法,只能從頭看看Hotseat上面那些快捷方式如何添加和加載。

 

2、Hotseat上Item如何生成

  Hotseat上面的快捷方式可以分為兩種,一種是普通快捷方式,可以在default_workspace里面配置。另外一個是全部應用列表的按鈕,這個實在Hotseat.java里面動態生成的。我們先看看普通的快捷方式如何生成,一般的快捷方式,通過加載default_workspace,這個過程跟workspace上面的按鈕一樣,前面已經有詳細分析,這里不多說。我們主要看看如何生成快捷方式的對象,其實這個跟workspace也是一樣,我們看看下面代碼:

//Edited by mythou
//http://www.cnblogs.com/mythou/
  public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) 
    {
     //.........for (int i=start; i<end; i++) 
        {
       //.........if(OWLLauncherDebug.HotSeat) Log.d("OWL_Launcher", "bindItems------->item="+item);
            
            switch (item.itemType) 
            {
                case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
                case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
           //生成Item的View對象 View shortcut
= createShortcut((ShortcutInfo)item);
           //添加到對應的screen里面 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX, item.cellY,
1, 1, false); break; } } }

  上面是Launcher里面的bindItems()方法的部分代碼,bindItems()這個方法,看過我前面分析Launcher數據加載的朋友應該不會陌生,這是綁定Launcher數據時候Launcher回調的一個方法,Hotseat的Item對象就在這里生成。調用了createShortcut()方法,下面我們看看createShortcut()是如何生成Item對象。

//Edited by mythou
//http://www.cnblogs.com/mythou/
  View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) 
    {
        if(OWLLauncherDebug.HotSeat) Log.d("OWL_Lancher", "addInScreen------->info="+info);
        //生成Item的快捷方式,Hotseat里面的按鈕也是BubbleTextView
        BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
        favorite.applyFromShortcutInfo(info, mIconCache);
        favorite.setOnClickListener(this);
        return favorite;
    }

  從這里可以看出,其實Hotseat里面的按鈕也是BubbleTextView的對象,也就是跟一般的workspace里面的按鈕一樣。對象生成以后會添加到對應的Screen里面。前面Hotseat的文章我也說過,Hotseat其實也是一個CellLayout的screen。在程序里面是標記為最后一個screen。

 

3、Item添加到Hotseat

//Edited by mythou
//http://www.cnblogs.com/mythou/
  void addInScreen(View child, long container, int screen, int x, int y, int spanX, int spanY,
            boolean insert) 
 {
        if(OWLLauncherDebug.HotSeat) Log.d(OWL, "addInScreen------->screen="+screen);
        final CellLayout layout;
     //針對Hotseat的對象做特殊處理 mythou
if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { if(OWLLauncherDebug.HotSeat) Log.d(OWL, "addInScreen------->CONTAINER_HOTSEAT"); layout = mLauncher.getHotseat().getLayout(); child.setOnKeyListener(null); //隱藏Hotseat文件夾標題 if (child instanceof FolderIcon) { // ((FolderIcon) child).setTextVisible(false); }if(OWLLauncherDebug.HotSeat) Log.d(OWL, "addInScreen------->CONTAINER_HOTSEAT screen="+screen); if (screen < 0) { screen = mLauncher.getHotseat().getOrderInHotseat(x, y); } else {//獲取child的位置,返回true添加成功,false失敗 這里其實是Hotseat里面Item的位置 x = mLauncher.getHotseat().getCellXFromOrder(screen); y = mLauncher.getHotseat().getCellYFromOrder(screen); } if(OWLLauncherDebug.HotSeat) Log.d(OWL, "addInScreen-------> x="+x + " y="+y); } }

  上面就是把Hotseat里面元素添加到Hotseat對應的CellLayout里面去,里面有一部分針對Hotseat元素的特殊處理,也就是剛開始說的隱藏文件夾標題以及技術元素位置的代碼。除了這些,我並沒有找到隱藏按鈕文字的方法。后來只能看看Hotseat里面另外一個按鈕(AllAPP)能不能顯示文字。

 

4、Hotseat的AllAPP按鈕

//Edited by mythou
//http://www.cnblogs.com/mythou/
  void resetLayout() 
    {
     //清空Hotseat里面所有的元素 mContent.removeAllViewsInLayout();
//添加ALLAPP按鈕 Context context = getContext(); LayoutInflater inflater = LayoutInflater.from(context); //修改為自己定義的button配置application, mythou BubbleTextView allAppsButton = (BubbleTextView) inflater.inflate(R.layout.application, mContent, false);
     //設置allapp按鈕的圖片 allAppsButton.setCompoundDrawablesWithIntrinsicBounds(
null, context.getResources().getDrawable(R.drawable.all_apps_button_icon), null, null); allAppsButton.setText(context.getString(R.string.all_apps_button_label)); allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label)); int width = allAppsButton.getWidth(); int heigh = allAppsButton.getHeight(); Log.d("OWL_HOTSEAT", "allAppsButton width="+width+" heigh="+heigh);
      
     //.........
  }

   上面是Hotseat.java里面的resetLayout()方法,前面也說了Hotseat里面的按鈕分為兩種,其中AllAPP按鈕是代碼里面生成的。最后也是調用addViewToCellLayout()方法,addViewToCellLayout()是CellLayout里面的方法,跟前面一樣,會添加到CellLayout里面。

 

5、Hotseat顯示Item標題

  前面說了半天都沒說到如何修改顯示標題。其實這個說起來很簡單,Hotseat里面的元素,每個都保存了Item的標題,我還特意把數據打印出來看了,因為剛開始以為系統直接把標題數據刪除了。之所以沒有顯示出來是因為Hotseat設置的高度太低,導致標題部分顯示被屏蔽了,很實用也很囧的方法。另外可能會有人要問,既然從顯示空間上限制了,為何還要加個判斷文件夾標題隱藏?這個問題答案,只要你看看文件夾高度就知道了,自己實踐一下才能印象深刻。

  我對Launcher的分析也有一段時間了,不過現在看來對這個程序還不了解,后面還是多花點時間,達到能隨心所欲地修改才行!最后謝謝女朋友大人給我提醒,讓我省了不少時間,O(∩_∩)O哈!

 

系列文章:

Android Launcher分析和修改1——Launcher默認界面配置(default_workspace)

Android Launcher分析和修改2——Icon修改、界面布局調整、壁紙設置

Android Launcher分析和修改3——Launcher啟動和初始化

Android Launcher分析和修改4——初始化加載數據

Android Launcher分析和修改5——HotSeat分析

Android Launcher分析和修改6——頁面滑動(PagedView)

Android Launcher分析和修改7——AllApp全部應用列表(AppsCustomizeTabHost)

Android Launcher分析和修改8——AllAPP界面拖拽元素(PagedViewWithDraggableItems)

Android Launcher分析和修改9——Launcher啟動APP流程

 

Edited by mythou

原創博文,轉載請標明出處:http://www.cnblogs.com/mythou/p/3211443.html 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM