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

Posted

技术标签:

【中文标题】使用 xamarin 表单删除 Android 上的顶部栏【英文标题】:remove top bar on Android with xamarin forms 【发布时间】:2017-06-11 17:08:43 【问题描述】:

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

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

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

RequestWindowFeature(WindowFeatures.NoTitle);

或在活动字符串中

Theme = "@style/MainTheme.FullScreen"

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

在我添加的 iO 上:

UIApplication.SharedApplication.SetStatusBarHidden(true, true);

并且可以工作...但是 android 他让我该死 :) 解决方案?

我使用 xamarin 表单 pcl

我的样式.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>

【问题讨论】:

您是否尝试将其添加到您的活动中? [Activity (Label = "@string/app_name", MainLauncher = true, Theme = "@android:style/Theme.Black.NoTitleBar.Fullscreen")] 是但不起作用,顶栏没有隐藏 【参考方案1】:
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());
    

【讨论】:

你绝对应该包含一些关于你的代码是做什么的描述。【参考方案2】:
 if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
 
     Window.AddFlags(WindowManagerFlags.TranslucentStatus);

     return; 
 

仅此一项并没有将视图置于状态栏下方,而是消除了状态栏下方的黑色空间(高度为 20px 左右)。

【讨论】:

【参考方案3】:

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

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

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

【讨论】:

这是官方的做法。我们的主题肯定有别的东西搞砸了。你能在你的styles.xml中添加你的完整主题吗? 也许我没有添加官方主题?我如何检查是否添加主题? 你能粘贴你的styles.xml中的内容吗? 什么?我只想看看你的styles.xml。打开你的styles.xml,Ctrl+a,Ctrl+c,回到这里,添加注释,Ctrl+v,回车 找不到与给定名称匹配的资源:attr 'windowFullscreen'。如何添加?抱歉,我是 xamarin 的初学者 :(【参考方案4】:

在我的情况下,它适用于:

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

【讨论】:

以上是关于使用 xamarin 表单删除 Android 上的顶部栏的主要内容,如果未能解决你的问题,请参考以下文章

我们如何在 Xamarin 表单中删除/自定义 Azure AD B2C 身份验证 webview 页面标题栏?

xamarin 表单信号器核心客户端处理程序不在 ios 上执行,但在 android 上执行

在 xamarin 表单上构建错误:Android

在 Android Xamarin 表单上禁用自动位置权限

如何在 Android 上管理从后台恢复的 Xamarin 表单应用程序?

带有 xamarin 表单的 xamarin 本机视图加载缓慢