Android从片段设置全屏 [英] Android set full screen from fragment

查看:235
本文介绍了Android从片段设置全屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题:我有一个Android应用程序,允许用户全屏显示,以提高可读性.全屏fab切换按钮放置在实际上包含读数的片段内.

This is my question: I have an android app which allows users to go full screen for a better readability. The full screen fab toggle button is placed inside a fragment which actually contains the readings.

使其变得简单:

  • 主要活动包含阅读片段
  • Readings Fragment包含一个用于切换全屏的fab按钮

要触发全屏显示,请使用以下代码段:

To trigger the full screen I use this snippet:

this.fullScreenFab.setOnClickListener(v -> {
    WindowManager.LayoutParams attrs = getActivity().getWindow().getAttributes();
    if (this.isFullScreen) {
        this.isFullScreen = false;
        ((AppCompatActivity) 
getActivity()).getSupportActionBar().show();
    getActivity().getWindow().getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

} else {
    this.isFullScreen = true;
    ((AppCompatActivity) getActivity()).getSupportActionBar().hide();
    getActivity().getWindow().getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LOW_PROFILE
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

}
getActivity().getWindow().setAttributes(attrs);
});

现在,它可以正常工作,除了状态栏和操作栏的空白会一直显示.活动进行到FS(甚至Android对此也发出警告),但是仍然保留了这两个元素所占用的空间.

Now, it works well, except that the status bar and action bar spaces keep showing. The activity goes FS (even Android warn me of this) but the space occupied by those two elements remain.

全屏已禁用:

已启用全屏显示:

如您所见,FS占据了顶部和底部,因此该片段不会真正全屏显示.

As you can see, the FS one has the top and bottom occupied, so the fragment does not go for a real full screen.

片段具有

android:fitsSystemWindows="true"

请发送帮助!提前致谢.瓦莱里奥(Valerio)

Send help please! Thanks in advance. Valerio

推荐答案

您应尝试使用此标志,因为该标志旨在删除状态栏和导航.

You should try using this flag as it is designed to remove status bar and navigation.

getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

注意::您需要在切换或关闭片段时手动清除此内容.否则,该全屏显示将一直适用,直到存在父活动为止.为此,

NOTE: You need to manually clear this while switching or closing fragment. Else this fullscreen will be applicable till the parent activity exists. To do so,

getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

此外,您可以使用 FLAG_FULLSCREEN .使用它可以有效地设置透明的通知栏,但状态栏上的图标仍会显示.

Also, you can use FLAG_FULLSCREEN. Using it will effectively set a transparent notification bar but the icons on the status bar will still show up.

这篇关于Android从片段设置全屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆