【问题标题】:Cannot truely hide the android statusbar working with Xamarin.Forms无法真正隐藏使用 Xamarin.Forms 的 android 状态栏
【发布时间】:2016-12-01 14:51:36
【问题描述】:

我正在开发一个 Xamarin.Forms 项目,我刚刚找到了使用以下代码从状态栏中隐藏图标的方法:

this.Window.AddFlags(WindowManagerFlags.Fullscreen | WindowManagerFlags.TurnScreenOn); 

现在我正在尝试隐藏蓝色状态栏,我尝试了 2 种不同的方法,但都不起作用: 1)将此代码添加到activity.cs:

SetStatusBarColor(Color.Transparent); 

2) 将这些行写在 style.xml 中所有可能的组合中(在 Resources/value 中):

<item name="colorPrimary">@android:color/transparent</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>

似乎没有任何效果,我该怎么办?

【问题讨论】:

  • 您的活动来自哪个对象?你在什么设备上测试? API 级别?
  • 其实我已经找到了出路!我要推它

标签: android xamarin.forms themes customization statusbar


【解决方案1】:

要隐藏状态栏并将应用全屏显示在您的 Android 清单中,您需要将您的应用设置为创建的主题

<application android:label="@string/ApplicationName" android:theme="@style/Theme" android:icon="@drawable/Icon" android:hardwareAccelerated="true"></application>

要在您的 Android 资源值文件夹中为 21 之前的 API 级别创建主题,请添加一个 styles.xml 文件。例如:

<?xml version="1.0" encoding="utf-8" ?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
  <style name="Theme" parent="Theme.Base">
  </style>
  <style name="Theme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowFullscreen">true</item>
    <item name="android:colorLongPressedHighlight">@android:color/transparent</item>
    <item name="android:colorActivatedHighlight">@android:color/transparent</item>
    <item name="android:windowBackground">@android:color/transparent</item>
  </style>
  <style name="Theme.Splash" parent="android:Theme">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
  </style>
</resources>

对于 Android API 21+ 创建另一个值文件夹 values-v21 并添加一个 styles.xml 文件,如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
  <!-- Base application theme for API 21+. This theme completely replaces 'Theme' from BOTH res/values/styles.xml on API 21+ devices. -->
  <style name="Theme" parent="Theme.Base">
    <item name="windowNoTitle">true</item>
    <item name="android:windowContentOverlay">@null</item>
  </style>
</resources>

另外,据我所知,这应该适用于所有 Xamarin Forms Android 应用 API 19+ 之前我没有尝试过的任何东西。

【讨论】:

    【解决方案2】:

    如果您使用导航,请尝试 NavigationPage.SetHasNavigationBar

    【讨论】:

      【解决方案3】:

      经过这么多研究,我从 JoeManke 在 xamarin 论坛上的 answer 那里得到了最好的方法。

      if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
              {
                  // Kill status bar underlay added by FormsAppCompatActivity
                  // Must be done before calling FormsAppCompatActivity.OnCreate()
                  var statusBarHeightInfo = typeof(FormsAppCompatActivity).GetField("statusBarHeight", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                  if (statusBarHeightInfo == null)
                  {
                      statusBarHeightInfo = typeof(FormsAppCompatActivity).GetField("_statusBarHeight", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                  }
                  statusBarHeightInfo?.SetValue(this, 0);
              }
      
              this.Window.AddFlags(WindowManagerFlags.Fullscreen | WindowManagerFlags.TurnScreenOn);
      

      【讨论】:

        猜你喜欢
        • 2013-08-06
        • 1970-01-01
        • 2021-10-11
        • 2016-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多