Android Launcher 在底部导航栏添加一个“☰”按钮,点击弹出全部应用

相关文件
frameworks/base/packages/SystemUI/res/layout/all_app.xml
frameworks/base/packages/SystemUI/res/values-sw400dp/config.xml
frameworks/base/packages/SystemUI/res/values-sw600dp/config.xml
frameworks/base/packages/SystemUI/res/values-sw720dp/config.xml
frameworks/base/packages/SystemUI/res/values-sw900dp/config.xml
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarFragment.java
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarInflaterView.java
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
packages / apps/Launcher3/src/com/android/launcher3/Launcher.java

思路

  • 1、 创建allApp布局
  • 2、 控制导航键显示位置
  • 3、 在Java代码中拿到它的xml资源文件
  • 4、 在Java代码中设置allApp图标
  • 5、 给allApp设置监听事件
  • 6、 实现弹出全部应用

实现

  • 第一步:创建allApp布局
    frameworks/base/packages/SystemUI/res/layout/all_app.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2016 The Android Open Source Project
     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
          http://www.apache.org/licenses/LICENSE-2.0
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<com.android.systemui.statusbar.policy.KeyButtonView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:systemui="http://schemas.android.com/apk/res-auto"
    android:id="@+id/all_app"
    android:layout_width="@dimen/navigation_key_width"
    android:layout_height="match_parent"
    android:layout_weight="0"
    android:scaleType="center"
    android:contentDescription="@string/accessibility_home"
    android:paddingStart="@dimen/navigation_key_padding"
    android:paddingEnd="@dimen/navigation_key_padding"
    />
  • 第二步:控制导航键显示位置
    frameworks/base/packages/SystemUI/res/values-sw400dp/config.xml
    frameworks/base/packages/SystemUI/res/values-sw600dp/config.xml
    frameworks/base/packages/SystemUI/res/values-sw720dp/config.xml
    frameworks/base/packages/SystemUI/res/values-sw900dp/config.xml
<resources>
    <!-- Nav bar button default ordering/layout -->
    <string name="config_navBarLayout" translatable="false">all_app;back,home,recent;right</string>
</resources>
  • 第三步:在Java代码中拿到它的xml资源文件
    frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarInflaterView.java
public static final String ALL_APP = "all_app";    //声明
private View createView(String buttonSpec, ViewGroup parent, LayoutInflater inflater) {
        if (HOME.equals(button)) {
            v = inflater.inflate(R.layout.home, parent, false);
        } else if(ALL_APP.equals(button)) {
            v = inflater.inflate(R.layout.all_app, parent, false);  /* 获取布局 */
        } else if (BACK.equals(button)) {
            v = inflater.inflate(R.layout.back, parent, false);
        } 
} 
  • 第四步:在Java代码中设置allApp图标
    frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
  private KeyButtonDrawable mAllAppIcon;      /* 创建图标对象 */
 public NavigationBarView(Context context, AttributeSet attrs) {
  /* 添加allApp给mButtonDispatchers */
        mButtonDispatchers.put(R.id.all_app, new ButtonDispatcher(R.id.all_app));
        mButtonDispatchers.put(R.id.back, new ButtonDispatcher(R.id.back));
        mButtonDispatchers.put(R.id.home, new ButtonDispatcher(R.id.home));
}
    /* 根据id拿到准确目标 */
    public ButtonDispatcher getAllAppButton() {
        return mButtonDispatchers.get(R.id.all_app);
    }
 private void updateIcons(Configuration oldConfig) {
        mVolumeSubIcon = getDrawable(R.drawable.ic_sysbar_volume_sub_button);
        mAllAppIcon = getDrawable(R.drawable.ic_menu);  /*获取资源文件 */
        mScreenshotIcon = getDrawable(R.drawable.ic_sysbar_capture_button);
}
    public void updateNavButtonIcons() {
        getVolumeSubButton().setImageDrawable(mVolumeSubIcon);
        getAllAppButton().setImageDrawable(mAllAppIcon);    /* 设置图标 */
        getScreenshotButton().setImageDrawable(mScreenshotIcon);
}
  • 第五步:给allApp设置监听事件
    frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarFragment.java
 private void prepareNavigationBarView() {
 	/* 王睿-设置监听 */
        ButtonDispatcher allAppButton = mNavigationBarView.getAllAppButton();
        allAppButton.setOnClickListener(this:: allAppClick);
}
   /* 王睿-点击事件 */
    private void allAppClick(View v) {
        Intent intent=new Intent("com.xxx.allApp");
        getContext().sendBroadcast(intent);
    }
  • 第六步:实现弹出全部应用
    packages / apps/Launcher3/src/com/android/launcher3/Launcher.java
@Override
    protected void onCreate(Bundle savedInstanceState) {
        registerReceiver(myReceiver, new IntentFilter("com.xxx.allApp"));	//动态注册广播
  }
 @Override
    public void onDestroy() {
  		unregisterReceiver(mScreenOffReceiver);
        unregisterReceiver(myReceiver);	//取消广播
        mWorkspace.removeFolderListeners();
  }
         /* 创建广播接收器 */
    private final BroadcastReceiver myReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            getStateManager().goToState(ALL_APPS);
        }
    };

Launcher 开发系列:
Launcher 在底部导航栏添加一个“☰”按钮,点击弹出全部应用
Launcher 修改底部导航虚拟按键的位置
Launcher 隐藏和开启底部虚拟按键(动态更改)
Launcher 去掉全部应用界面的搜索框
Launcher 点击鼠标右键时,显示菜单栏
Launcher 自定义一个虚拟按键实现返回主页和打开全部应用两个功能

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>