• 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. }







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

  • 相关阅读:
    metinfo 6.0.0 任意文件读取漏洞复现
    内存管理机制
    JavaScript
    AI不离谱,大语言模型ChatMusician可以理解曲谱生成AI音乐
    QT 中多线程实现方法总结
    c#弹性和瞬态故障处理库Polly
    B/S架构:手术室麻醉信息管理系统源码
    【Docker 内核详解】namespace 资源隔离(四):Mount namespace & Network namespace
    机器学习实战-系列教程7:SVM分类实战2线性SVM(鸢尾花数据集/软间隔/线性SVM/非线性SVM/scikit-learn框架)项目实战、代码解读
    数据库 SQL高级查询语句:聚合查询,多表查询,连接查询
  • 原文地址:https://blog.csdn.net/zsyf33078/article/details/133902983