Android導航欄功能項的顯示與屏蔽介紹

 Android 的導航欄有諸多功能,例入 截屏,音量加,音量減,最近任務,菜單.返回,主頁面,輸入法開關 ......

代碼源路徑:frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\phone\NavigationBarView.java

    public ButtonDispatcher getScreenshotButton() {
        return mButtonDispatchers.get(R.id.screenshot);
    }
 
    public ButtonDispatcher getVolumeAddButton() {
        return mButtonDispatchers.get(R.id.volume_add);
    }
 
    public ButtonDispatcher getVolumeSubButton() {
        return mButtonDispatchers.get(R.id.volume_sub);
    }
 
    public ButtonDispatcher getRecentsButton() {
        return mButtonDispatchers.get(R.id.recent_apps);
    }
 
    public ButtonDispatcher getMenuButton() {
        return mButtonDispatchers.get(R.id.menu);
    }
 
    public ButtonDispatcher getBackButton() {
        return mButtonDispatchers.get(R.id.back);
    }
 
    public ButtonDispatcher getHomeButton() {
        return mButtonDispatchers.get(R.id.home);
    }
 
    public ButtonDispatcher getImeSwitchButton() {
        return mButtonDispatchers.get(R.id.ime_switcher);
    }
 
    public ButtonDispatcher getAccessibilityButton() {
        return mButtonDispatchers.get(R.id.accessibility_button);
    }

Android 即在此類中完成對 Button圖標 的選擇 . 

若需要展示/隱藏對應的 功能選項時,需要在此處修改:

private void prepareNavigationBarView() {
        mNavigationBarView.reorient();
 
        ButtonDispatcher recentsButton = mNavigationBarView.getRecentsButton();
        recentsButton.setOnClickListener(this::onRecentsClick);
        recentsButton.setOnTouchListener(this::onRecentsTouch);
        recentsButton.setLongClickable(true);
        recentsButton.setOnLongClickListener(this::onLongPressBackRecents);
 
        ButtonDispatcher backButton = mNavigationBarView.getBackButton();
        backButton.setLongClickable(true);
        backButton.setOnLongClickListener(this::onLongPressBackRecents);
 
        ButtonDispatcher homeButton = mNavigationBarView.getHomeButton();
        homeButton.setOnTouchListener(this::onHomeTouch);
        homeButton.setOnLongClickListener(this::onHomeLongClick);
 
        ButtonDispatcher accessibilityButton =         mNavigationBarView.getAccessibilityButton();
        accessibilityButton.setOnClickListener(this::onAccessibilityClick);
        accessibilityButton.setOnLongClickListener(this::onAccessibilityLongClick);
        updateAccessibilityServicesState(mAccessibilityManager);
 
        ButtonDispatcher screenshotButton = mNavigationBarView.getScreenshotButton();
        screenshotButton.setOnClickListener(this:: screenshotClick);
        screenshotButton.setOnTouchListener(this:: screenshotTouch);
        boolean isShow=Settings.System.getInt(getContext().getContentResolver(), Settings.System.SCREENSHOT_BUTTON_SHOW, 1) == 1;
        if(isShow){
            screenshotButton.setVisibility(View.VISIBLE);
        }else{
            screenshotButton.setVisibility(View.GONE);
        }
 
        ButtonDispatcher volumeAddButton=mNavigationBarView.getVolumeAddButton();
        ButtonDispatcher volumeSubButton=mNavigationBarView.getVolumeSubButton();
        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);
        }
    }

可以將對應的Button 設置 setVisibility(View.GONE / View.INVISIBLE / View.VISIBLE); 

 當加上setVisibility時,UI界面還沒顯示時,需要註意config.xml中是否有寫進去需要的Button . 

frameworks\base\packages\SystemUI\res\values\config.xml

<string name="config_navBarLayout" translatable="false">left;volume_sub,back,home,recent,volume_add;right</string>

需要註意的是,config中配置的順序 即為 UI的顯示順序 .

提一嘴,如果需要用到屏蔽狀態欄以及任務欄,可以將如下status_bar_height 的高度設為0.

 frameworks\base\core\res\res\values\dimens.xml

    <!-- Height of the status bar -->
    <dimen name="status_bar_height">0dp</dimen>

到此這篇關於Android導航欄功能項的顯示與屏蔽介紹的文章就介紹到這瞭,更多相關Android導航欄內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: