在片段中隐藏状态栏或使片段全屏显示

时间:2019-12-04 07:10:21

标签: android user-interface android-fragments android-fullscreen

我有一个带有底部导航设置的kotlin应用程序。

我目前有5个片段[ProfileFragment, SearchFragment, HomeFragment, SettingsFragment, WebViewFragment]

所有这些都是空白片段。但是在我的个人资料片段中,我在页面的上半部分展示了Panaroma小部件

Profile Page

我知道使我的整个应用全屏显示,但是在其他片段上,内容将被隐藏在带缺口的显示屏下。从内容上讲,我的意思是我要用的雇主的徽标,这是毫无疑问的。

所以,我尝试了另一种方法。我将应用程序全屏显示,并在缺口下的任何地方添加了填充。现在,碰巧有各种各样的电话,没有缺口。内容看起来异常填充,因为没有缺口。

如果我对缺口显示进行了调整,则非缺口显示会出现问题。反之亦然。

我想,为什么不让全屏显示所有活动,如果我可以拉伸ProfileFragment覆盖状态栏或隐藏状态栏,那将是一个完美的解决方案。

是否可以执行以下任一操作?

  • 隐藏ProfileFragment上的状态栏
  • 将片段拉伸到屏幕顶部
  • 使片段全屏显示,而不会中断底部导航

3 个答案:

答案 0 :(得分:1)

您可以尝试在“活动”中添加以下代码:

// Hide the status bar.
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
// Remember that you should never show the action bar if the status bar is hidden, so hide that too if necessary.
actionBar?.hide()

此处有更多信息:https://developer.android.com/training/system-ui/status#kotlin

答案 1 :(得分:1)

AndroidX(支持库)具有内置的OnApplyWindowInsetsListener,可帮助您以与设备兼容的方式确定窗口的顶部,例如顶部(状态栏)或底部(例如键盘)。

由于插图适用于API 21+,因此您必须手动获取低于它的插图。这是Java(v8)中的一个示例,希望您能理解它:

public class MainActivity extends AppCompatActivity {
    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        View mainContainer = findViewById(R.id.main_container);   // You layout hierarchy root

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ViewCompat.setOnApplyWindowInsetsListener(mainContainer , (v, insets) -> {
                int statusBarHeight = 0;
                if (!isInFullscreenMode(getWindow())) statusBarHeight = insets.getSystemWindowInsetTop();

                // Get keyboard height
                int bottomInset = insets.getSystemWindowInsetBottom();

                // Add status bar and bottom padding to root view
                v.setPadding(0, statusBarHeight, 0, bottomInset);
                return insets;
            });
        } else {
            int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
            int statusBarHeight = 0;
            if (resourceId > 0 && !isInFullscreenMode(getWindow())) {
                statusBarHeight = getResources().getDimensionPixelSize(resourceId);
            }

            View decorView = getWindow().getDecorView();
            decorView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
                Rect r = new Rect();
                //r will be populated with the coordinates of your view that area still visible.
                decorView.getWindowVisibleDisplayFrame(r);

                //get screen height and calculate the difference with the useable area from the r
                int height = decorView.getContext().getResources().getDisplayMetrics().heightPixels;
                int bottomInset = height - r.bottom;

                // if it could be a keyboard add the padding to the view
                // if the use-able screen height differs from the total screen height we assume that it shows a keyboard now
                //set the padding of the contentView for the keyboard
                mainContainer.setPadding(0, statusBarHeight, 0, bottomInset);
            });
        }
        ...
    }

    public static boolean isInFullscreenMode(Window activityWindow) {
        return (activityWindow.getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) == WindowManager.LayoutParams.FLAG_FULLSCREEN;
    }
}

请注意,要使底部插图正常工作,您必须告诉Android您的活动是可调整大小的,因此在AndroidManifest.xml中:

<application
    ...>
    <activity
        android:name=".MainActivity"
        ...
        android:windowSoftInputMode="adjustResize"/>
    ...
</application>

答案 2 :(得分:0)

如果使用AppCompatActivity,也可以使用:

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

在onCreate方法中。