Hide Android Navigation Bar in React Native

后端 未结 7 1158
名媛妹妹
名媛妹妹 2020-12-10 02:27

How can I hide the Android Navigation Bar in React Native?

I\'m referring to the bar at the bottom of the screen that has the software back button and home button, n

7条回答
  •  生来不讨喜
    2020-12-10 02:51

    If you're looking to achieve this permanently you'll have to hide the Navbar when the app is created and when it comes back into focus.

    To do so, add this in your MainActivity.java:

    ...
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends ReactActivity {
    
        ...
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            hideNavigationBar();
        }
    
        @Override
        public void onWindowFocusChanged(boolean hasFocus) {
            super.onWindowFocusChanged(hasFocus);
            if (hasFocus) {
                hideNavigationBar();
            }
        }
    
        private void hideNavigationBar() {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        }
    }
    

    You may want to make it "immersive" so that the user can still access the navigation bar by pulling from the bottom of the screen. To do so, add the View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY flag:

    ...
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends ReactActivity {
    
        ...
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            hideNavigationBar();
        }
    
        @Override
        public void onWindowFocusChanged(boolean hasFocus) {
            super.onWindowFocusChanged(hasFocus);
            if (hasFocus) {
                hideNavigationBar();
            }
        }
    
        private void hideNavigationBar() {
            getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
    
        }
    }
    

    You can find more options on the android documentation.

提交回复
热议问题