AOSP6.0.1 launcher3入門篇—hotseat相關實現
阿新 • • 發佈:2018-12-15
在安卓桌面程式的主介面我們可以看到是由QsbSearchBar(上方搜尋框)、Workspace(頁檢視空間)、pageIndicator(頁指示器)、hotseat(底部檢視空間)四個部分組成,它們是基於DragLayer 層的基礎上進行顯示(註釋掉 res/ 橫屏模式layout-land 或 豎屏模式layout-port/launcher.xml中 android:id="@+id/drag_layer"對應的欄位 會發現桌面上沒有載入任何部件,DragLayer層屬於最外層的佈局,用來協調處理它的子view的動作事件,這裡不再描述DragLayer層相關資訊),下面說明關於hotseat佈局的相關描述以及allapps呼叫的CellLayout的相關實現位置。
首先進入hotseat.java檔案:
hotseat類繼承於FrameLayout型別,在hotseat.java中可以到hotseat佈局的相關資訊(長按事件的繫結,hotseat事件的截獲,X、Y座標獲取等),如果在launcher類(launcher.java)的事件函式中註釋掉hotseat相關的點選、長按事件,那麼hotseat佈局內的app將變為擺設(無法觸發事件)。
進入protected void onFinishInflate()函式: DeviceProfile grid = mLauncher.getDeviceProfile(); 首先獲取裝置的相關資訊(行、列規格、桌面的方向等),桌面的排列方式和裝置型號相關 (比如nexus5手機螢幕是1080P xxhdpi,規格為4*4 呼叫default_workspace_4x4.xml檔案) mContent = (CellLayout) findViewById(R.id.layout); 呼叫的是res/layout/hotseat.xml檔案 android:id="@+id/layout"對應的是res/layout, 如果你想做hotseat(包括allapps呼叫的cellLayout佈局)相關的修改 (比如修改預設圖示、事件觸發圖示,這只是其中一小部分), 檢視layout相關的資料夾內的xml將是不錯的選擇(layout、layout-land等, 所有包含layout名稱的資料夾) hotseat佈局其實是一個CellLayout檢視,桌面內所有的檢視空間都有著相同的概念, 他們由相對應的控制器來管理... 繼續向下看: if (grid.isLandscape && !grid.isLargeTablet) { mContent.setGridSize(1, (int) grid.inv.numHotseatIcons); } else { mContent.setGridSize((int) grid.inv.numHotseatIcons, 1); } 這裡設定了hotseat佈局的行和列(可以設定成2行2列或者更多,預設是1行) mContent.setIsHotseat(true); 這一句用來設定是否顯示hotseat佈局裡的app是否顯示對應的app名稱, 修改為mContent.setIsHotseat(false);即可顯示除allapps以外的所有app名稱 後面描述allapps按鈕名稱的新增方法
進入void resetLayout()函式: mContent.removeAllViewsInLayout(); 首先清除掉allapps按鈕和allapps按鈕進入的CellLayout空間 // Add the Apps button Context context = getContext(); LayoutInflater inflater = LayoutInflater.from(context); TextView allAppsButton = (TextView) inflater.inflate(R.layout.all_apps_button, mContent, false); Drawable d = context.getResources().getDrawable(R.drawable.all_apps_button_icon); mLauncher.resizeIconDrawable(d); allAppsButton.setCompoundDrawables(null, d, null, null); 這些是關於allapps按鈕的載入, 以及重設圖示大小和圖示的事件觸發範圍(感覺是整個桌面佈局內的圖示和範圍都重新 重新整理了一邊),如果註釋掉resetLayout()函式的呼叫將不會載入allapps按鈕和重新整理圖示等相關工作 allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label)); 這裡呼叫res/layout/all_apps_search_bar.xml, 找到android:contentDescription="@string/all_apps_button_label"欄位便可發現, 只要在res下搜尋string相關的xml檔案,即可找到想要的資訊 下面重點來了,設定allapps名稱的地方之一: if (mLauncher != null) { mLauncher.setAllAppsButton(allAppsButton); allAppsButton.setOnTouchListener(mLauncher.getHapticFeedbackTouchListener()); allAppsButton.setOnClickListener(mLauncher); allAppsButton.setOnLongClickListener(mLauncher); allAppsButton.setOnFocusChangeListener(mLauncher.mFocusHandler); allAppsButton.setText("allapps按鈕"); 在if (mLauncher != null) 中增加上面這段語句,即可增加allapps的應用名稱 } mContent.addViewToCellLayout(allAppsButton, -1, allAppsButton.getId(), lp, true); 這一條語句是把allAppsButton 按鈕嵌入CellLayout佈局
下面解析AllAppsContainerView.java檔案(管理allapps按鈕進入的桌面):
進入public void setSearchBarController(AllAppsSearchBarController searchController)函式:
mSearchBarContainerView.setVisibility(/*View.VISIBLE*/View.GONE);
這句話修改為View.GONE即可隱藏QsbSearchBar(allapps內的上方搜尋框)
進入public boolean onLongClick(View v)函式:
在函式內最上方增加 if (true)return true;
取消長按按鈕
在allapps內的CellLayout空間里長按螢幕中的app會出現一個新的拖動頁面,用來把指定
app拖動到主桌面,如果不需要這個功能隱藏掉即可
關於allapps按鈕進入的桌面目前只需要修改這麼多,如果需要修改其他功能,參考相應的函式進行修改即可。