【问题标题】:Hide Status bar in a fragment or make the fragment full screen在片段中隐藏状态栏或使片段全屏
【发布时间】:2023-03-06 18:23:02
【问题描述】:

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

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

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

我知道让我的整个应用全屏显示,但是在其他片段上,内容会隐藏在缺口显示下。至于内容,我的意思是我的雇主的标志,他想要的,没有失败。

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

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

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

有没有办法执行以下任一操作?

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 这个方法确实有帮助,但只是在一定程度上。如果我更改为另一个片段或活动,它会保持全屏显示。我想,我必须在每个活动中添加、显示和隐藏状态条码。
    【解决方案2】:

    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>
    

    【讨论】:

      【解决方案3】:

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

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

      在 onCreate 方法中。

      【讨论】:

      • 我在一个片段中。我没有使用操作栏
      猜你喜欢
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多