Android App隐藏Navigation Bar

1. 简介

    在Android4.4.2(KITKAT<Build.VERSION_CODES.KITKAT>)之前,只能设置:           

      1)View.SYSTEM_UI_FLAG_HIDE_NAVIGATION

           其缺点是当Touch Screen时,Navigation bar将显示出来。

    从Android4.4.2起,可以设置:

     1)View.SYSTEM_UI_FLAG_HIDE_NAVIGATION

     2)View.SYSTEM_UI_FLAG_IMMERSIVE

         同时设置以上两个参数,即使Touch Screen时,Navigation bar也不会显示出来。

2. 实现代码

private static Handler sHandler;

protected void onCreate(Bundle savedInstanceState){  
    super.onCreate(savedInstanceState);   

    sHandler = new Handler();
    
    sHandler.post(mHideRunnable); // hide the navigation bar

    final View decorView = getWindow().getDecorView();
    decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
        {
            @Override
            public void onSystemUiVisibilityChange(int visibility)
            {
                sHandler.post(mHideRunnable); // hide the navigation bar
            }
        });        
}

Runnable mHideRunnable = new Runnable() {
    @Override
    public void run() {
        int flags;  
        int curApiVersion = android.os.Build.VERSION.SDK_INT;
        // This work only for android 4.4+
        if(curApiVersion >= Build.VERSION_CODES.KITKAT){
            // This work only for android 4.4+
            // hide navigation bar permanently in android activity
            // touch the screen, the navigation bar will not show
            flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                  | View.SYSTEM_UI_FLAG_IMMERSIVE
                  | View.SYSTEM_UI_FLAG_FULLSCREEN;
                
        }else{
            // touch the screen, the navigation bar will show
            flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        }
        
        // must be executed in main thread :)
        getWindow().getDecorView().setSystemUiVisibility(flags);
    }
};  



展开阅读全文

没有更多推荐了,返回首页