1. setContentView()之前,加入
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- /* 隐藏标题栏 */
- supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
-
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
2. 在onCreate()方法中使用
- public class MainActivity extends AppCompatActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- /* 隐藏标题栏 */
- getSupportActionBar().hide();
- }
直接在onCreate中调用
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
能隐藏 状态栏,但是依旧有状态栏黑边
使用如下方法可以彻底全屏显示
1. super.onCreate和setContentView()之前,加入如下代码:
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- /* 在super.onCreate()之前添加代码块 start */
- /* 隐藏标题栏 */
- supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
- /* 隐藏状态栏 */
- supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
-
- WindowManager.LayoutParams lp = getWindow().getAttributes();
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
- lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
- }
- getWindow().setAttributes(lp);
- /* 在super.onCreate()之前添加代码块 end */
-
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
2. 添加实现函数onWindowFocusChanged函数
- /* 添加函数onWindowFocusChanged */
- @Override
- public void onWindowFocusChanged(boolean hasFocus) {
- super.onWindowFocusChanged(hasFocus);
- if (hasFocus) {
- 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);
- }
- }
完整代码如下:
- package com.example.fullscreentest;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.os.Build;
- import android.os.Bundle;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
-
- public class MainActivity extends AppCompatActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- /* 在super.onCreate()之前添加代码块 start */
- /* 隐藏标题栏 */
- supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
- /* 隐藏状态栏 */
- supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
-
- WindowManager.LayoutParams lp = getWindow().getAttributes();
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
- lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
- }
- getWindow().setAttributes(lp);
- /* 在super.onCreate()之前添加代码块 end */
-
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
-
- /* 添加函数onWindowFocusChanged */
- @Override
- public void onWindowFocusChanged(boolean hasFocus) {
- super.onWindowFocusChanged(hasFocus);
- if (hasFocus) {
- 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);
- }
- }
- }