使用 xamarin 表单删除 Android 上的顶部栏

remove top bar on Android with xamarin forms

我尝试了几种解决方案,包括:

<item name="android:actionBarSize">0dp</item>

var activity = (Activity)Forms.Context;
this.Window.AddFlags(WindowManagerFlags.Fullscreen);

RequestWindowFeature(WindowFeatures.NoTitle);

或在activity字符串中

Theme = "@style/MainTheme.FullScreen"

但是我找不到任何有效的解决方案,或者更确切地说,我删除了歌词、电池时间等,但我仍然保持顶部栏不变,我怎样才能完全删除它?

在 iOs 我添加了:

UIApplication.SharedApplication.SetStatusBarHidden(true, true);

并且有效...但是 android 他让我该死的 :) 解决方案?

我使用 xamarin 表单 pcl

我的styles.xaml

    <?xml version="1.0" encoding="utf-8" ?>
<resources>
  <style name="MainTheme" parent="MainTheme.Base">
  </style>
  <!-- Base theme applied no matter what API -->
  <style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
    <item name="windowNoTitle">true</item>
    <!--We will be using the toolbar so no need to show ActionBar-->
    <item name="windowActionBar">false</item>
    <!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette -->
    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">#2196F3</item>
    <!-- colorPrimaryDark is used for the status bar -->
    <!--<item name="colorPrimaryDark">#0084CA</item>-->
    <!-- colorAccent is used as the default value for colorControlActivated
         which is used to tint widgets -->
    <item name="colorAccent">#2196F3</item>
    <!-- You can also set colorControlNormal, colorControlActivated
         colorControlHighlight and colorSwitchThumbNormal. -->
    <item name="windowActionModeOverlay">true</item>
    <!-- default -->
    <item name="android:buttonStyle">@style/NoShadowButton</item>

    <item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
  </style>

  <style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">#2196F3</item>
  </style>
  <style name="NoShadowButton" parent="android:style/Widget.Button">
    <item name="android:stateListAnimator">@null</item>
  </style>
</resources>

我在 Xamarin 论坛上回答了这个类似的问题:

将这两项添加到您给定的主题中 activity:

<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>

https://forums.xamarin.com/discussion/comment/248555

编辑:为了让您更轻松,请将样式更改为此 -

<resources>
  <style name="MainTheme.FullScreen" parent="MainTheme.Base">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
  </style>
  <style name="MainTheme" parent="MainTheme.Base">
  </style>
  <!-- Base theme applied no matter what API -->
  <style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
    <!--We will be using the toolbar so no need to show ActionBar-->
    <item name="windowActionBar">false</item>
    <!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette -->
    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">#2196F3</item>
    <!-- colorPrimaryDark is used for the status bar -->
    <!--<item name="colorPrimaryDark">#0084CA</item>-->
    <!-- colorAccent is used as the default value for colorControlActivated
     which is used to tint widgets -->
    <item name="colorAccent">#2196F3</item>
    <!-- You can also set colorControlNormal, colorControlActivated
     colorControlHighlight and colorSwitchThumbNormal. -->
    <item name="windowActionModeOverlay">true</item>
    <!-- default -->
    <item name="android:buttonStyle">@style/NoShadowButton</item>

    <item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
  </style>

  <style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">#2196F3</item>
  </style>
  <style name="NoShadowButton" parent="android:style/Widget.Button">
    <item name="android:stateListAnimator">@null</item>
  </style>
</resources>
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {

    this.Window.AddFlags(WindowManagerFlags.Fullscreen | WindowManagerFlags.TurnScreenOn);
        if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
        {
            var stBarHeight = typeof(FormsAppCompatActivity).GetField("statusBarHeight", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            if (stBarHeight == null)
            {
                stBarHeight = typeof(FormsAppCompatActivity).GetField("_statusBarHeight", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            }
            stBarHeight?.SetValue(this, 0);
        }
        base.OnCreate(bundle);


        global::Xamarin.Forms.Forms.Init(this, bundle);

        LoadApplication(new App());
    }
}
 if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
 {
     Window.AddFlags(WindowManagerFlags.TranslucentStatus);

     return; 
 }

单独这样做并没有将视图放在状态栏下方,而是去掉了状态栏下方的黑色 space(高度为 20px 左右)。

在我的例子中,它适用于:

<?xml version="1.0" encoding="utf-8" ?>
<resources>

  <style name="MainTheme" parent="MainTheme.Base">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
  </style>
  <!-- Base theme applied no matter what API -->
  <style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
    <item name="windowNoTitle">true</item>
    <!--We will be using the toolbar so no need to show ActionBar-->
    <item name="windowActionBar">false</item>
    <!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette -->
    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">#2196F3</item>
    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">#1976D2</item>
    <!-- colorAccent is used as the default value for colorControlActivated
         which is used to tint widgets -->
    <item name="colorAccent">#FF4081</item>
    <!-- You can also set colorControlNormal, colorControlActivated
         colorControlHighlight and colorSwitchThumbNormal. -->
    <item name="windowActionModeOverlay">true</item>

    <item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
  </style>

  <style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">#FF4081</item>
  </style>
</resources>