当前位置:   article > 正文

Android应用开发(38)全屏显示隐藏标题栏、状态栏和导航栏_安卓隐藏系统状态栏代码

安卓隐藏系统状态栏代码

Android应用开发学习笔记——目录索引

一、隐藏标题栏

1. setContentView()之前,加入

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. /* 隐藏标题栏 */
  4. supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. }

2.  在onCreate()方法中使用

  1. public class MainActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. /* 隐藏标题栏 */
  7. getSupportActionBar().hide();
  8. }

二、隐藏状态栏和导航栏

直接在onCreate中调用

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);

能隐藏 状态栏,但是依旧有状态栏黑边

使用如下方法可以彻底全屏显示

1. super.onCreate和setContentView()之前,加入如下代码:

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. /* 在super.onCreate()之前添加代码块 start */
  4. /* 隐藏标题栏 */
  5. supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
  6. /* 隐藏状态栏 */
  7. supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
  8. getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
  9. WindowManager.LayoutParams lp = getWindow().getAttributes();
  10. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
  11. lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
  12. }
  13. getWindow().setAttributes(lp);
  14. /* 在super.onCreate()之前添加代码块 end */
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. }

  2. 添加实现函数onWindowFocusChanged函数

  1. /* 添加函数onWindowFocusChanged */
  2. @Override
  3. public void onWindowFocusChanged(boolean hasFocus) {
  4. super.onWindowFocusChanged(hasFocus);
  5. if (hasFocus) {
  6. View decorView = getWindow().getDecorView();
  7. decorView.setSystemUiVisibility(
  8. View.SYSTEM_UI_FLAG_LAYOUT_STABLE
  9. | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
  10. | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
  11. | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
  12. | View.SYSTEM_UI_FLAG_FULLSCREEN
  13. | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
  14. }
  15. }

完整代码如下:

  1. package com.example.fullscreentest;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Build;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.Window;
  7. import android.view.WindowManager;
  8. public class MainActivity extends AppCompatActivity {
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. /*super.onCreate()之前添加代码块 start */
  12. /* 隐藏标题栏 */
  13. supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
  14. /* 隐藏状态栏 */
  15. supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
  16. getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
  17. WindowManager.LayoutParams lp = getWindow().getAttributes();
  18. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
  19. lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
  20. }
  21. getWindow().setAttributes(lp);
  22. /*super.onCreate()之前添加代码块 end */
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25. }
  26. /* 添加函数onWindowFocusChanged */
  27. @Override
  28. public void onWindowFocusChanged(boolean hasFocus) {
  29. super.onWindowFocusChanged(hasFocus);
  30. if (hasFocus) {
  31. View decorView = getWindow().getDecorView();
  32. decorView.setSystemUiVisibility(
  33. View.SYSTEM_UI_FLAG_LAYOUT_STABLE
  34. | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
  35. | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
  36. | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
  37. | View.SYSTEM_UI_FLAG_FULLSCREEN
  38. | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
  39. }
  40. }
  41. }



或者

  1. protected void onCreate(Bundle savedInstanceState) {
  2. super.onCreate(savedInstanceState);
  3. setContentView(R.layout.activity_main);
  4. //设置布局延伸到刘海屏内,没此处设置会导致小米手机顶部导航栏显示黑色。
  5. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
  6. try {
  7. WindowManager.LayoutParams lp = getWindow().getAttributes();
  8. lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
  9. getWindow().setAttributes(lp);
  10. } catch (Exception e) {
  11. e.printStackTrace();
  12. }
  13. }
  14. getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  15. getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
  16. getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
  17. getWindow().getDecorView().setSystemUiVisibility(
  18. View.SYSTEM_UI_FLAG_LAYOUT_STABLE
  19. | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
  20. | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
  21. | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
  22. | View.SYSTEM_UI_FLAG_FULLSCREEN
  23. | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.INVISIBLE);
  24. //设置状态栏和导航栏颜色为透明
  25. getWindow().setStatusBarColor(Color.TRANSPARENT);
  26. getWindow().setNavigationBarColor(Color.TRANSPARENT);





点此查看Android应用开发学习笔记的完整目录

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/791082
推荐阅读
相关标签
  

闽ICP备14008679号