29

我需要在我的应用程序中全屏显示屏幕。为此,我正在使用以下代码:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    requestWindowFeature(Window.FEATURE_NO_TITLE)
    window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN)
    setContentView(R.layout.activity_camera_photo)

但是,该WindowManager.LayoutParams.FLAG_FULLSCREEN标志已被弃用。

我的应用支持 Android Lollipop (API 21) 到 Android R (API 30)。使屏幕全屏显示的正确方法是什么?

4

7 回答 7

40

科特林

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.layout_container)
    @Suppress("DEPRECATION")
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        window.insetsController?.hide(WindowInsets.Type.statusBars())
    } else {
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )
    }
}

如果这没有帮助,请尝试android:fitsSystemWindows="true"在布局文件中删除

JAVA

class Activity extends AppCompatActivity {

@Override
@SuppressWarnings("DEPRECATION")
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_container);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        final WindowInsetsController insetsController = getWindow().getInsetsController();
        if (insetsController != null) {
            insetsController.hide(WindowInsets.Type.statusBars());
        }
    } else {
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN
        );
    }
}
}
于 2020-07-15T09:29:33.363 回答
11

我有像user924这样的问题

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.WindowInsetsController com.android.internal.policy.DecorView.getWindowInsetsController()' on a null object reference

我可以通过 setContentView. 此外,通常情况下,全屏屏幕不仅没有状态栏,也没有导航栏。此外,仅有hide()方法是不够的。如果我们只放这一行,当我们向下滑动屏幕看到状态栏时,它会下降,但不会再上升。通过设置systemBarBehavior,我们可以让状态栏和导航栏只在我们熟悉的全屏滑动时临时出现。

setContentView(R.layout.YOUR_LAYOUT)

//Set full screen after setting layout content
@Suppress("DEPRECATION")
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    val controller = window.insetsController

    if(controller != null) {
        controller.hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
        controller.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
    }
} else {
    window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
}
于 2021-01-12T19:00:29.710 回答
8

对于 API >= 30,请使用WindowInsetsController.hide()

window.insetsController.hide(WindowInsets.Type.statusBars())
于 2020-07-10T14:14:47.167 回答
2

该代码可在具有 Android 4.0 (API 14) 至 10 (API 29) 的真实手机和具有 Android R (API 30) 的 SDK 手机模拟器上运行。

在样式资源中编写启动活动的主题。

 <!--Splash screen theme-->
  <style name="SplashTheme" parent="@style/Theme.AppCompat.NoActionBar">
  <item name="android:windowFullscreen">true</item>
  <item name="android:windowBackground">@color/colorSplashBackground</item>
  </style>

够了。无需代码为启动活动隐藏栏。

主要活动。

使用主题。

<!-- Base application theme. -->
    <style name="myAppTheme" parent="@style/Theme.AppCompat.NoActionBar">
    <!-- Customize your theme here. -->
    </style>

在 MainActivity 类中编写代码。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    // Hide bar when you want. For example hide bar in landscape only
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        hideStatusBar_AllVersions();
    }
    setContentView( R.layout.activity_main );
    // Add your code
    }

实施方法。

@SuppressWarnings("deprecation")
void hideStatusBar_Deprecated() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {  // < 16
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    } else {  // 16...29
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);

        ActionBar ab = getSupportActionBar();
        if (ab != null) { ab.hide(); }

        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
}

@TargetApi(Build.VERSION_CODES.R) // >= 30
void hideStatusBar_Actual() {
    View decorView = getWindow().getDecorView();
    decorView.getWindowInsetsController().hide(WindowInsets.Type.statusBars());
}

void hideStatusBar_AllVersions(){
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
        hideStatusBar_Deprecated();
    } else {
        hideStatusBar_Actual();
    }
}
于 2021-07-27T09:03:41.540 回答
0
   override fun onAttachedToWindow() {
    super.onAttachedToWindow()
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        val controller = window.insetsController
        if (controller != null) {
            controller.hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
            controller.systemBarsBehavior =
                WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
        }
    }
}

@Suppress("DEPRECATION")
private fun makeFullScreen() {
    // Remove Title
    requestWindowFeature(Window.FEATURE_NO_TITLE)
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )
    }
    // Hide the toolbar
    supportActionBar?.hide()
}
于 2021-12-08T10:11:49.340 回答
0
fullScreenButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {

                    WindowInsetsController controller = getWindow().getInsetsController();
                    //BEFORE TOGGLE
//                    System.out.println(controller.getSystemBarsAppearance());
//                    System.out.println(controller.getSystemBarsBehavior());
                    if(controller != null) {
                        if (controller.getSystemBarsBehavior() == 0) {
                            controller.hide(WindowInsets.Type.statusBars() | WindowInsets.Type.navigationBars());
                            controller.setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
                        } else {
                            controller.show(WindowInsets.Type.statusBars() | WindowInsets.Type.navigationBars());
                            controller.setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_BARS_BY_TOUCH);
                        }
                    }
                } else {
                    WindowManager.LayoutParams attrs = getWindow().getAttributes();
                    attrs.flags ^= WindowManager.LayoutParams.FLAG_FULLSCREEN;
                    getWindow().setAttributes(attrs);
                }
                //AFTER TOGGLE
                //                    System.out.println(controller.getSystemBarsAppearance());
//                    System.out.println(controller.getSystemBarsBehavior());
            }
        });
于 2021-05-28T14:34:43.090 回答
0
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {     
 window.insetsController!!.hide(android.R.style.Theme_NoTitleBar_Fullscreen)
}
于 2022-01-14T12:53:10.913 回答