• 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应用开发学习笔记的完整目录

  • 相关阅读:
    Linux文件权限
    MySQL ——单行处理函数实例练习
    SpringBoot 如何实现文件上传和下载
    Autosar基础——RTE简介
    优化python中os模块遍历文件夹时的排序问题
    糯米粉可以做什么好吃的 糯米粉做法大全
    python实例代码介绍python基础知识
    Linux常用监控命令(笔试面试常考)
    快速搭建Vue前端框架
    【QT】QT Designer控件随窗口大小自适应
  • 原文地址:https://blog.csdn.net/zsyf33078/article/details/133902983