Android はヘッドステータスバーを非表示にします

序文

APP を研究し、たくさんのデモを書いているときに、上部にステータス バー (ActionBar) があることがわかりました。これは見た目が悪く、使用しないときにスペースを占有するため、それを非表示にする方法を見つけなければなりません。

まずは公式サイトで紹介されている方法を貼り付けます

ステータス バーを非表示にする | Android 開発者 | Android 開発者

Android 4.1以降、ActivityがApplicationから継承する場合は、公式メソッドを使用してステータスバーを非表示にします

    View decorView = getWindow().getDecorView();
    // Hide the status bar.
    int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
    decorView.setSystemUiVisibility(uiOptions);
    // Remember that you should never show the action bar if the
    // status bar is hidden, so hide that too if necessary.
    ActionBar actionBar = getActionBar();
    actionBar.hide();
    

しかし!

公式サイトで紹介されているメソッドがエラーになっていたのでデバッグで確認したところ、 getActionBar() メソッドを使ってnullを取得していることが分かり、いろいろ調べたところ、Activity が Appcompat を継承している場合、 ActionBar を取得するには getSupportActionBar() を使用する必要があります

            View decorView = getWindow().getDecorView();
            // Hide the status bar.
            int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
            // Remember that you should never show the action bar if the
            // status bar is hidden, so hide that too if necessary.
            //ActionBar actionBar = getActionBar();
            ActionBar actionBar = getSupportActionBar();
            if(actionBar != null){
                actionBar.hide();
            }

おすすめ

転載: blog.csdn.net/TDSSS/article/details/126352205