Android 11.0 在导航栏左下角添加一个按钮,使它具有点击弹出allApp的功能

相关文件
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);
++        }
++    };

猜你喜欢

转载自blog.csdn.net/qq_27494201/article/details/125196779