ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] ![](https://img.kancloud.cn/f1/4c/f14cb32859275a89010cec94f29468e5_264x475.png) # 设置状态栏和导航栏 UI Flag名称|作用|说明 ---|---|--- View.SYSTEM\_UI\_FLAG\_FULLSCREEN|隐藏状态栏|一般同时隐藏ActionBar View.SYSTEM\_UI\_FLAG\_LAYOUT\_FULLSCREEN和View.SYSTEM\_UI\_FLAG\_LAYOUT\_STABLE|应用主体内容占用状态栏(用于更改状态栏颜色)|两个Flag必须同时使用,然后调用Window的setStatusBarColor()方法 View.SYSTEM\_UI\_FLAG\_HIDE\_NAVIGATION|隐藏导航栏| View.SYSTEM\_UI\_FLAG\_LAYOUT\_HIDE\_NAVIGATION和View.SYSTEM\_UI\_FLAG\_LAYOUT\_STABLE|应用主体内容占用导航栏(用于更改导航栏颜色)|两个Flag必须同时使用,然后调用Window的setNavigationBarColor()方法 ## 隐藏状态栏 ```java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); View decorView = getWindow().getDecorView(); // 隐藏状态栏 int option = View.SYSTEM_UI_FLAG_FULLSCREEN; decorView.setSystemUiVisibility(option); // 隐藏ActionBar ActionBar actionBar = getSupportActionBar(); actionBar.hide(); } } ``` ## 设置状态栏颜色 ```java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (Build.VERSION.SDK_INT >= 21) { View decorView = getWindow().getDecorView(); int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE; decorView.setSystemUiVisibility(option); // 此处设置为透明状态栏,也可设置为其他颜色 getWindow().setStatusBarColor(Color.TRANSPARENT); } // 隐藏ActionBar ActionBar actionBar = getSupportActionBar(); actionBar.hide(); } } ``` ## 隐藏导航栏 ```java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); View decorView = getWindow().getDecorView(); // 隐藏导航栏 int option = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION; decorView.setSystemUiVisibility(option); } } ``` ## 设置导航栏颜色 ```java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (Build.VERSION.SDK_INT >= 21) { View decorView = getWindow().getDecorView(); int option = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE; decorView.setSystemUiVisibility(option); // 此处设置为透明导航栏,也可设置为其他颜色 getWindow().setNavigationBarColor(Color.TRANSPARENT); } } } ``` # 沉浸式模式 ![](https://img-blog.csdn.net/20160820202910617) ```java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus && Build.VERSION.SDK_INT >= 19) { View decorView = getWindow().getDecorView(); decorView.setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); } } } ``` # 刘海屏适配 ## 设置应用内容延伸到刘海区 属性名称|作用|说明 ---|---|--- LAYOUT\_IN\_DISPLAY\_CUTOUT\_MODE\_DEFAULT|默认属性,应用的内容在竖屏模式下自动延伸到刘海区域,在横屏模式下则不会延伸到刘海区域|在小米9上测试此属性在竖屏模式下也不会延伸到刘海区 LAYOUT\_IN\_DISPLAY\_CUTOUT\_MODE\_SHORT\_EDGES|不管手机处于横屏还是竖屏模式,都会允许应用程序的内容延伸到刘海区域 LAYOUT\_IN\_DISPLAY\_CUTOUT\_MODE\_NEVER|永远不允许应用程序的内容延伸到刘海区域 ```java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 这里需要调用这两行代码,来去除旋转屏幕时状态栏出现的问题 requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main); ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { actionBar.hide(); } if (Build.VERSION.SDK_INT >= 28) { WindowManager.LayoutParams params = getWindow().getAttributes(); params.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES; getWindow().setAttributes(params); } } @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus && Build.VERSION.SDK_INT >= 19) { View decorView = getWindow().getDecorView(); decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); } } } ``` ## 获取安全显示区域 获取安全显示区域的偏移量,再设置给控件即可。 ```java rootLayout.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() { @Override public WindowInsets onApplyWindowInsets(View view, WindowInsets windowInsets) { DisplayCutout displayCutout = windowInsets.getDisplayCutout(); if (displayCutout != null) { int left = displayCutout.getSafeInsetLeft(); int top = displayCutout.getSafeInsetTop(); int right = displayCutout.getSafeInsetRight(); int bottom = displayCutout.getSafeInsetBottom(); ConstraintLayout.LayoutParams topParams = (ConstraintLayout.LayoutParams) topButton.getLayoutParams(); topParams.setMargins(left, top, right, bottom); ConstraintLayout.LayoutParams leftParams = (ConstraintLayout.LayoutParams) leftButton.getLayoutParams(); leftParams.setMargins(left, top, right, bottom); } return windowInsets.consumeSystemWindowInsets(); } }); ``` 参考文档: [Android状态栏微技巧,带你真正理解沉浸式模式](https://blog.csdn.net/guolin_blog/article/details/51763825) [Android 9.0系统新特性,对刘海屏设备进行适配](https://blog.csdn.net/guolin_blog/article/details/103112795)