StatusBar 和 NavigationBar 的 隐藏与浸透式

/**
 * View.SYSTEM_UI_FLAG_FULLSCREEN:Activity全屏显示,且状态栏被隐藏覆盖掉。
 * View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN:Activity全屏显示,但状态栏不会被隐藏覆盖,状态栏依然可见,Activity顶端布局部分会被状态栏遮住。
 *
 * View.SYSTEM_UI_FLAG_HIDE_NAVIGATION       Activity全屏显示,且底部导航栏被隐藏覆盖掉。
 * View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION     Activity全屏显示,但底部导航栏不会被隐藏覆盖,导航栏依然可见,Activity底端布局部分会被导航栏遮住。
 *
 * View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY   底部向上,顶部向下滑动,显示出导航栏,状态栏的时候,画面不会被挤
 * View.SYSTEM_UI_FLAG_IMMERSIVE   底部向上,顶部向下滑动,显示出导航栏,状态栏的时候,画面会被挤
 */
package caifc.android.com.camera2test;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.view.View;

public class BarUtils {
    /**
     * 隐藏状态栏
     * @param activity
     */
    public static void hideStatusBar(Activity activity){
        View decorView = activity.getWindow().getDecorView();
        int option = decorView.getSystemUiVisibility();
        option |= View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        decorView.setSystemUiVisibility(option);
        activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
        ActionBar actionBar = activity.getActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
    }

    /**
     * 沉浸状态栏
     * @param activity
     */
    public static void immerseStatusBar(Activity activity){
        View decorView = activity.getWindow().getDecorView();
        int option = decorView.getSystemUiVisibility();
        option |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        decorView.setSystemUiVisibility(option);
        activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
        ActionBar actionBar = activity.getActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
    }


    /**
     * 隐藏导航栏
     * @param activity
     */
    public static void hideNavigationBar(Activity activity){
        View decorView = activity.getWindow().getDecorView();
        int option = decorView.getSystemUiVisibility();
        option |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        decorView.setSystemUiVisibility(option);
        activity.getWindow().setNavigationBarColor(Color.TRANSPARENT);
        ActionBar actionBar = activity.getActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
    }

    /**
     * 沉浸导航栏
     * @param activity
     */
    public static void immerseNavigationBar(Activity activity){
        View decorView = activity.getWindow().getDecorView();
        int option = decorView.getSystemUiVisibility();
        option |= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        decorView.setSystemUiVisibility(option);
        activity.getWindow().setNavigationBarColor(Color.TRANSPARENT);
        ActionBar actionBar = activity.getActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
    }
}

通过 Style配置文件实现  只有沉浸式

        false
        true

        true
        @android:color/transparent

        true
        @android:color/transparent

 

 通过Window设置(暂时没有找到 隐藏 NavigationBar 的方法)

//会隐藏状态栏, 内容填充状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //状态栏透明,内容填充状态栏
        this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //底部导航栏透明, 内容填充导航栏
        this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

 

你可能感兴趣的:(Android)