本文实例讲述了Android7.0开发实现Launcher3去掉应用抽屉的方法。分享给大家供大家参考,具体如下:
年初做过一个项目,有一个需求就是需要将桌面变为单层不需要二级菜单。最近几次有小伙伴有这个问我这个解决办法。现在我将分享给大家。
<强>先上效果图:强>
<强>功能分解强>
1。去除Allapp键,调整HotSeat布局
2. 将所有应用摆在发射器第一层
3.去掉长按时删除选项
<>强解决方案强>
按照6.0 Launcher3的模式,添加一个开关,控制是否去掉抽屉。
LauncherAppState类:单例模式,主要在启动的时候用,他初始化了一些对象,并且注册了广播监听器和ContentObserver。为了能灵活切换模式,在此类中添加静态开关。
Launcher3 android \ src \ com \ \ Launcher3 \ LauncherAppState.java:
公共静态布尔isDisableAllApps () {//返回false> 公共布尔isAllAppsButtonRank (int排名){//添加@ { 如果(LauncherAppState.isDisableAllApps ()) { 返回错误; }//添加@} 返回排名==mAllAppsButtonRank; } >之前3)我没有抽屉,所以不需要allapp按钮。在HotSeat里面去掉allapp键的加载,在HotSeat。java的空白resetLayout()中初始化HotSeat布局。在我时停止加载Allapp按钮。
Launcher3 android \ src \ com \ \ Launcher3 \ HotSeat。java→resetLayout ():
空白resetLayout () { mContent.removeAllViewsInLayout ();//添加@ { 如果(LauncherAppState.isDisableAllApps ()) {//添加}@//添加应用程序按钮 上下文语境=getContext (); LayoutInflater增压泵=LayoutInflater.from(上下文); TextView allAppsButton=(TextView) inflater.inflate(出来。all_apps_button mContent,假); 可拉的d=context.getResources () .getDrawable (R.drawable.all_apps_button_icon); mLauncher.resizeIconDrawable (d); allAppsButton。setCompoundDrawables (null, d,空,空); allAppsButton.setContentDescription (context.getString (R.string.all_apps_button_label)); allAppsButton。setOnKeyListener(新HotseatIconKeyEventListener ()); 如果(mLauncher !=null) { mLauncher.setAllAppsButton (allAppsButton); allAppsButton.setOnTouchListener (mLauncher.getHapticFeedbackTouchListener ()); allAppsButton.setOnClickListener (mLauncher); allAppsButton.setOnLongClickListener (mLauncher); allAppsButton.setOnFocusChangeListener (mLauncher.mFocusHandler); }//注意:这样做是为了确保hotseat总是在的方向//hotseat为了无论取向,他们补充道 int x=getCellXFromOrder (mAllAppsButtonRank); int y=getCellYFromOrder (mAllAppsButtonRank); CellLayout。LayoutParams lp=new CellLayout.LayoutParams (x, y, 1,1); lp。canReorder=false; mContent。addViewToCellLayout (allAppsButton 1 allAppsButton.getId ()、lp, true); } }//别漏了这里的} >之前
InvariantDeviceProfile。java Launcher3进行布局初始化的一个类。
在有allapp按钮时HotSeat里HotSeat图标数量为五个,没有allapp按钮时HotSeat图标数量应为四个。
Launcher3 android \ src \ com \ \ Launcher3 \ InvariantDeviceProfile.java:
1)先加个宏控
//添加@ { 私人布尔hasDA=LauncherAppState.isDisableAllApps ();//添加}@ >之前2)去掉抽屉时,HotSeat的格数为四格,所以不能抛出异常。(numHotseatIcons为偶时不抛异常)
InvariantDeviceProfile ():
InvariantDeviceProfile (String n, w,浮动浮动h, int, int, int fr, int fc, int maapc,//确保我们有一个奇数的hotseat物品(因为我们需要将所有应用程序) 如果(hs % 2==0,,! hasDA){//在无抽屉情况下不抛异常 把新RuntimeException(“所有设备资料必须有奇数个hotseat空间”); } name=n; …… } >之前3)去掉抽屉的情况下加载不同的布局
getPredefinedDeviceProfiles ():
ArrayListgetPredefinedDeviceProfiles () { ArrayList Android7.0开发实现Launcher3去掉应用抽屉的方法详解