赞
踩
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);
- }
- }
- }
或者
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- //设置布局延伸到刘海屏内,没此处设置会导致小米手机顶部导航栏显示黑色。
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
- try {
- WindowManager.LayoutParams lp = getWindow().getAttributes();
- lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
- getWindow().setAttributes(lp);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
- getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
- getWindow().getDecorView().setSystemUiVisibility(
- View.SYSTEM_UI_FLAG_LAYOUT_STABLE
- | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
- | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
- | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
- | View.SYSTEM_UI_FLAG_FULLSCREEN
- | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.INVISIBLE);
- //设置状态栏和导航栏颜色为透明
- getWindow().setStatusBarColor(Color.TRANSPARENT);
- getWindow().setNavigationBarColor(Color.TRANSPARENT);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。