Android9底部导航栏出现空白按钮问题分析

2023-09-20 10:21:22

底部导航栏的初始化

NavigationBarFragment.onCreateView()初始化时渲染创建了navigation_bar

@Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.navigation_bar, container, false);
    }

这个navigation_bar布局:
很直接的是一个NavigationBarView内部嵌套NavigationBarInflaterView

<com.android.systemui.statusbar.phone.NavigationBarView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:systemui="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@drawable/system_bar_background">

    <com.android.systemui.statusbar.phone.NavigationBarInflaterView
        android:id="@+id/navigation_inflater"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</com.android.systemui.statusbar.phone.NavigationBarView>

进入NavigationBarView初始化:

创建了大量按键的按键矢量图对象,和按键调度对象。

public class NavigationBarView extends FrameLayout implements PluginListener<NavGesture> {
	private final SparseArray<ButtonDispatcher> mButtonDispatchers = new SparseArray<>();
	// 大量的keybuttondrawable成员变量。即按键的图片资源
	private KeyButtonDrawable mBackIcon;
	...
    private KeyButtonDrawable mHomeDefaultIcon, mHomeCarModeIcon;
    private KeyButtonDrawable mRecentIcon;
	....
	
	public NavigationBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // 为所有的keybuttondrawable加载drawable矢量图资源
        reloadNavIcons();
        // 以按键id- buttonDispatcher对象为键值对,全部存入SparseArray数组中
        mButtonDispatchers.put(R.id.back, new ButtonDispatcher(R.id.back));
        mButtonDispatchers.put(R.id.home, new ButtonDispatcher(R.id.home));
        mButtonDispatchers.put(R.id.recent_apps, new ButtonDispatcher(R.id.recent_apps));
        mButtonDispatchers.put(R.id.menu, new ButtonDispatcher(R.id.menu));
        mButtonDispatchers.put(R.id.ime_switcher, new ButtonDispatcher(R.id.ime_switcher));
       
    }

进入NavigationBarView的onFinishInflater

显示器尺寸和横竖屏布局的确认

@Override
    public void onFinishInflate() {
    	// 绑定NavigationInflaterView对象,并将初始化好的buttondispatcher数组传值
        mNavigationInflaterView = findViewById(R.id.navigation_inflater);
        mNavigationInflaterView.setButtonDispatchers(mButtonDispatchers);
        ....
        // 更新横竖屏的布局加载
        updateRotatedViews();
 }
private void updateCurrentView() {
        final int rot = mDisplay.getRotation();
        // NavigationBarView共有0,90,180,270四个角度的背景,横竖两种layout布局。
        // 根据显示display的旋转角度来指定其中一个显示。
        for (int i = 0; i < 4; i++) {
            mRotatedViews[i].setVisibility(View.GONE);
        }
        .........
    }

进入NavigationBarInflaterView

所有button的显示是在这个view中确定的

@Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        // 贴上四个布局文件,并绑定子view
        inflateChildren(); 
        clearViews();
        // 解析配置文件渲染
        // getDefaultLayout获取到R.string.config_navBarLayoutQuickstep或R.string.config_navBarLayout
        // 是字符串left;volume_sub,back,home,recent,volume_add,screenshot;right[.1W]
        inflateLayout(getDefaultLayout()); 
    }
protected void inflateLayout(String newLayout) {
       .......
		// 用;号取出三组按钮组
        String[] sets = newLayout.split(GRAVITY_SEPARATOR, 3);
        .....
        // 这三组分别是left, center, right,主要按键都在center组中。
        String[] start = sets[0].split(BUTTON_SEPARATOR);
        String[] center = sets[1].split(BUTTON_SEPARATOR); 
        String[] end = sets[2].split(BUTTON_SEPARATOR);
        // Inflate these in start to end order or accessibility traversal will be messed up.
        // 渲染三组,left是空格,center是音量,right是比较复杂的menu,ime,rotate等但均不显示
        inflateButtons(start, mRot0.findViewById(R.id.ends_group), isRot0Landscape, true); // 宽屏
        inflateButtons(start, mRot90.findViewById(R.id.ends_group), !isRot0Landscape, true);
        inflateButtons(center, mRot0.findViewById(R.id.center_group), isRot0Landscape, false);
        inflateButtons(center, mRot90.findViewById(R.id.center_group), !isRot0Landscape, false);
        addGravitySpacer(mRot0.findViewById(R.id.ends_group));// 插入空格符
        addGravitySpacer(mRot90.findViewById(R.id.ends_group));
        inflateButtons(end, mRot0.findViewById(R.id.ends_group), isRot0Landscape, false);
        inflateButtons(end, mRot90.findViewById(R.id.ends_group), !isRot0Landscape, false);
        updateButtonDispatchersCurrentView();
    }

NavigationBarInflaterView加载单个的button

到这里逐个加载配置中的button后,导航栏的显示已差不多完成。
点击事件不需要额外添加监听,每个按钮都是keyButtonview对象,自带了虚拟键值和onTouch事件。

protected View inflateButton(String buttonSpec, ViewGroup parent, boolean landscape,
            boolean start) {
        ..
        // createView非常关键,根据条件获取按钮的资源。
        // 加载了按键的显示布局xml,并且带有systemui:keycode虚拟键值。
        // 每个按键布局都是一个keyButtonView,其中自带了点击touch事件处理,发送xml中的虚拟键值给系统。
        View v = createView(buttonSpec, parent, inflater);
        if (v == null) return null;
        v = applySize(v, buttonSpec, landscape, start);// 含有[]如,right[.1W]则重新分配尺寸
        parent.addView(v);  // 将按键填入父控件中进行显示
        addToDispatchers(v); //加入mButtonDispatchers集合中。
        .......
        return v;
    }

回到NavigationFragment的创建流程

在onViewCreated()流程中
prepareNavigationBarView()方法注册了一些点击事件,最重要的是更新横竖布局和按钮显示,通过sysprop确定了音量加减的显示。
最后notifyNavigationBarScreenOn()方法再次更新了按钮的显示。

private void prepareNavigationBarView() {
		// 再次刷新横竖屏布局的显示和隐藏,并且更新导航按钮的显示隐藏。
        mNavigationBarView.reorient();
        .................
        // 音量加减键
        ButtonDispatcher volumeAddButton=mNavigationBarView.getVolumeAddButton();
        ButtonDispatcher volumeSubButton=mNavigationBarView.getVolumeSubButton();
        // prop中为true
        boolean isShowVolumeButton="true".equals(SystemProperties.get("ro.rk.systembar.voiceicon","true"));
        if(isShowVolumeButton){
            volumeAddButton.setVisibility(View.VISIBLE);
            volumeSubButton.setVisibility(View.VISIBLE);
        }else{
            volumeAddButton.setVisibility(View.GONE);
            volumeSubButton.setVisibility(View.GONE);
        }
        if (getContext().getResources().getConfiguration().smallestScreenWidthDp < 400) {
            volumeAddButton.setVisibility(View.GONE);
            volumeSubButton.setVisibility(View.GONE);
        }
    }
// 再次更新NavButtonIcons;
private void notifyNavigationBarScreenOn() {
        mNavigationBarView.updateNavButtonIcons();
    }

多次调用NavigationBarView的刷新

把构造方法中实例化的keybuttondrawable图像逐个设置给buttondispatcer,按钮有了具体的矢量图象。
最终结果就是显示back , home, recents三个键,外加上述提到的音量加减,其他全部隐藏

public void updateNavButtonIcons() {
        ......
        getHomeButton().setImageDrawable(homeIcon);
        getBackButton().setImageDrawable(backIcon);
        getVolumeAddButton().setImageDrawable(mVolumeAddIcon);
        getVolumeSubButton().setImageDrawable(mVolumeSubIcon);
        getScreenshotButton().setImageDrawable(mScreenshotIcon);
        ......
        getBackButton().setVisibility(disableBack      ? View.INVISIBLE : View.VISIBLE);
        getHomeButton().setVisibility(disableHome      ? View.INVISIBLE : View.VISIBLE);
        getRecentsButton().setVisibility(disableRecent ? View.INVISIBLE : View.VISIBLE);
    }

初始化流程的错误追踪。

在底部导航栏的初始化中,刚开始加载的配置是back;home ;contextual导致一直在追查音量加减在哪里加入的,实际上整个初始化流程多次刷新,最后一次加载的配置是left;volume_sub,back,home,recent,volume_add,screenshot;right[.1W]。

空格按键的问题定位。

不断的尝试中发现,安卓9盒子HDMI输出给18.5寸一体机时,调用的默认布局配置是layout-sw600dp,源码中特意增加的layout布局,其中仅有一个文件navigation_layout_rot90.xml,即横屏的NavigationInflaterView布局。
且这个布局内容很奇怪。ends_group组和center_group组都是linearlayout和默认layout中的资源文件不一样,但是改动或者同步都会导致导航栏只剩一个home键。

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:systemui="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/nav_buttons"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:id="@+id/ends_group"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:clipChildren="false" />
        <LinearLayout
            android:id="@+id/center_group"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="horizontal"
            android:clipChildren="false" />
    </FrameLayout>
</FrameLayout>

临时解决办法

三组按钮,left(空格), center(关键功能键), right(复杂功能全都隐藏)。right组中的所有按钮都是隐藏的,而且基本没见过,直接取消right组按钮的加载后,空格按钮就消除了,在NavigationBarInflaterView中

protected void inflateLayout(String newLayout) {
     	....
     	// 取消掉right这组的按钮加载
        // inflateButtons(end, mRot0.findViewById(R.id.ends_group), isRot0Landscape, false);
        // inflateButtons(end, mRot90.findViewById(R.id.ends_group), !isRot0Landscape, false);
        updateButtonDispatchersCurrentView();
    }
更多推荐

新概念英语(第二册)复习——Lesson 6 - Lesson10

前言在学习6-10之前,确保1-5已经可以脱口而出,否则不需要学习6-10文章目录前言Lesson6-PercyButtons原文译文单词Lesson7-Toolate原文译文单词Lesson8-Thebestandtheworst原文译文单词Lesson9-Acoldwelcome译文单词Lesson10-NotFo

数组初学者向导:使用Python从零开始制作经典战舰游戏

引言战舰游戏,一个广受欢迎的经典游戏,为玩家提供了策略与猜测的完美结合。这个游戏的核心思想是通过猜测敌方船只的位置并尝试击沉它们来赢得比赛。在这篇文章中,我们将使用Python语言和数组来构建这款游戏,让你更加了解数组的操作和实用性。1.游戏概述在战舰游戏中,每位玩家都有一个10x10的网格,可以放置5艘船只。这些船只

C语言实现三子棋游戏(详解)

目录引言:1.游戏规则:2.实现步骤:2.1实现菜单:2.2创建棋盘并初始化:2.3绘制棋盘:2.4玩家落子:2.5电脑落子:2.6判断胜负:3.源码:结语:引言:《三子棋》是一款古老的民间传统游戏,又被称为OOXX棋、黑白棋,井字棋,九宫棋等。游戏分为双方对战,双方依次在九宫格棋盘上摆放棋子,率先将自己的三个棋子连成

Linux 下的 10 个 PDF 软件

本文[1]是我们正在进行的有关Linux顶级工具系列的延续,在本系列中,我们将向您介绍最著名的Linux系统开源工具。随着互联网上越来越多地使用可移植文档格式(PDF)文件来获取在线书籍和其他相关文档,拥有PDF查看器/阅读器对于桌面Linux发行版非常重要。有几种可以在Linux上使用的PDF查看器/阅读器,它们都提

Excel VBA 变量,数据类型&常量

几乎所有计算机程序中都使用变量,VBA也不例外。在过程开始时声明变量是一个好习惯。这不是必需的,但有助于识别内容的性质(文本,​​数据,数字等)在本教程中,您将学习-一、VBA变量变量是存储在计算机内存或存储系统中的特定值。以后,您可以在代码中使用该值并执行。计算机将从系统中获取该值并显示在输出中。必须为每个变量指定一

Java内存模型

一、运行时数据区域的分区JVM虚拟机在执行Java程序的过程中会把它管理的内存划分成若干个不同的数据区域。1.1运行时数据区域的划分JVM虚拟机在执行Java程序的过程中会把它管理的内存划分成若干个不同的数据区域。JDK1.8之前分为:线程共享(Heap堆区、MethodArea方法区)、线程私有(虚拟机栈、本地方法栈

统信UOS系统开发笔记(七):在统信UOS系统上使用linuxdeployqt发布qt程序

若该文为原创文章,转载请注明原文出处本文章博客地址:https://hpzwl.blog.csdn.net/article/details/131411975红胖子(红模仿)的博文大全:开发技术集合(包含Qt实用技术、树莓派、三维、OpenCV、OpenGL、ffmpeg、OSG、单片机、软硬结合等等)持续更新中…(点

QT5 QCamera摄像头

文章目录前言一、QCamera类二、QCameraViewfinder类三、QCameraInfo类四、QCameraImageCapture类总结前言本篇文章我们来讲解QT如何使用通过QCamera调用摄像头。本篇文章的话就围绕QT5来展开讲解,QT6的话已经更新了多媒体的调用方式,这里我们以后再进行讲解。一、QCa

微服务整合Gateway网关

✅作者简介:大家好,我是Leo,热爱Java后端开发者,一个想要与大家共同进步的男人😉😉🍎个人主页:Leo的博客💞当前专栏:微服务探索之旅✨特色专栏:MySQL学习🥭本文内容:微服务整合Gateway网关🖥️个人小站:个人博客,欢迎大家访问📚个人知识库:知识库,欢迎大家访问大家好,我是Leo🫣🫣🫣,

C/C++算法入门 | 简单模拟

不爱生姜不吃醋⭐️如果本文有什么错误的话欢迎在评论区中指正与其明天开始,不如现在行动!文章目录🌴前言🌴一、害死人不偿命的(3n+1)猜想1.题目(PATB1001)2.思路3.代码实现🌴二、挖掘机技术哪家强1.题目(PATB1032)2.思路3.代码实现🌴总结🌴前言本文内容是关于C/C++算法入门的简单模拟题

3D模型格式转换工具HOOPS Exchange协助Epic Games实现CAD数据轻松导入虚幻引擎

一、面临的挑战EpicGames最为人所知的身份可能是广受欢迎的在线视频游戏Fortnite的开发商,但它也是虚幻引擎背后的团队,虚幻引擎是一种实时3D创作工具,为世界领先的游戏提供动力,并且也被电影电视、建筑、汽车、制造、模拟等领域采用。从很多方面来说,这种进展并不令人意外。由于视频游戏行业在渲染和可视化方面往往处于

热文推荐