對 UI 顯示設定變更的回應

本課程將說明如何註冊事件監聽器,以便讓應用程式接收通知 系統 UI 瀏覽權限變更。如果您要 將使用者介面的其他部分與系統資訊列的隱藏/顯示功能同步處理。

註冊監聽器

如要收到系統 UI 瀏覽權限變更的通知,請註冊 View.OnSystemUiVisibilityChangeListener的瀏覽次數。 這通常是您用來控制導覽瀏覽權限的檢視畫面。

例如,您可以將這段程式碼加進活動的 onCreate() 方法:

Kotlin

window.decorView.setOnSystemUiVisibilityChangeListener { visibility ->
    // Note that system bars will only be "visible" if none of the
    // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
    if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) {
        // TODO: The system bars are visible. Make any desired
        // adjustments to your UI, such as showing the action bar or
        // other navigational controls.
    } else {
        // TODO: The system bars are NOT visible. Make any desired
        // adjustments to your UI, such as hiding the action bar or
        // other navigational controls.
    }
}

Java

View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener
        (new View.OnSystemUiVisibilityChangeListener() {
    @Override
    public void onSystemUiVisibilityChange(int visibility) {
        // Note that system bars will only be "visible" if none of the
        // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
            // TODO: The system bars are visible. Make any desired
            // adjustments to your UI, such as showing the action bar or
            // other navigational controls.
        } else {
            // TODO: The system bars are NOT visible. Make any desired
            // adjustments to your UI, such as hiding the action bar or
            // other navigational controls.
        }
    }
});

一般來說,最好讓 UI 與系統資訊列的變更保持同步 曝光率。舉例來說,您可以使用此事件監聽器在以下位置隱藏和顯示動作列: 並隱藏狀態列