How to set fullscreen in Android R?

喜你入骨 提交于 2020-12-24 23:56:12

问题


I need to put a screen in fullscreen in my app. For this I am using this code:

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)

However, the WindowManager.LayoutParams.FLAG_FULLSCREEN flag is deprecated.

My app supports Android Lollipop (API 21) to Android R (API 30). What is the correct way to make a screen go fullscreen?


回答1:


KOTLIN

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
        )
    }
}

if this doesn't help, try to remove android:fitsSystemWindows="true" in the layout file

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
        );
    }
}
}



回答2:


For API >= 30, use WindowInsetsController.hide():

window.insetsController.hide(WindowInsets.Type.statusBars())


来源:https://stackoverflow.com/questions/62835053/how-to-set-fullscreen-in-android-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!