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

Android 在片段中隐藏状态栏或使片段全屏显示,android,user-interface,android-fragments,android-fullscreen,Android,User Interface,Android Fragments,Android Fullscreen,我有一个带有底部导航设置的kotlin应用程序 我目前有5个片段[ProfileFragment、SearchFragment、homeframent、setingsframent、WebViewFragment] 所有这些都只是空白片段。但在我的个人资料片段中,我展示了页面上半部分的panaroma小部件 我知道如何使我的整个应用程序全屏显示,但在其他片段上,内容将隐藏在凹口显示下。说到内容,我指的是我雇主的标志,他一定要 所以,我尝试了另一种方法。我让应用程序全屏显示,并在任何地方添加填充

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

我目前有5个片段
[ProfileFragment、SearchFragment、homeframent、setingsframent、WebViewFragment]

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

我知道如何使我的整个应用程序全屏显示,但在其他片段上,内容将隐藏在凹口显示下。说到内容,我指的是我雇主的标志,他一定要

所以,我尝试了另一种方法。我让应用程序全屏显示,并在任何地方添加填充,在缺口下隐藏内容。现在,有各种各样的手机,没有刻痕。内容看起来异常低沉,因为,嗯,没有刻痕

如果我调整凹口显示,非凹口显示将出现问题。反之亦然

因此,我想,如果我可以拉伸ProfileFragment以覆盖状态栏,或者隐藏状态栏,那么为什么不让我的应用程序中的所有活动都全屏显示,这将是一个完美的解决方案

有没有一种方法可以执行以下任一操作

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

您可以尝试在活动中添加此代码:

// 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()

此处的详细信息:

您可以尝试在活动中添加此代码:

// 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()

此处的详细信息:

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

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

在onCreate方法中。

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

if(getSupportActionBar() != null) {
   getSupportActionBar().hide();
}
在onCreate方法中。

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

由于插页适用于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>

...
AndroidX(支持库)具有内置功能,可帮助您以设备兼容的方式确定窗口插页,如顶部(状态栏)或底部插页(即键盘)

由于插页适用于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>

...

此方法确实有帮助,但仅在一定程度上有所帮助。如果我切换到另一个片段或活动,它将保持全屏。我想,我必须在每个活动中添加、显示和隐藏状态条形码。这种方法确实有用,但只是在一定程度上有用。如果我切换到另一个片段或活动,它将保持全屏。我想,我必须在每个活动中添加、显示和隐藏状态条形码。我在一个片段中。我没有使用actionbarI,我在一个片段中。我不是在用actionbar