简体   繁体   中英

Cannot truely hide the android statusbar working with Xamarin.Forms

I'm working on a Xamarin.Forms project and I've just found the way to hide the icons from the status bar with this code:

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

Now i'm trying to hide the blue status bar, and I've tried 2 different ways, both not working: 1) Add this code to the activity.cs:

SetStatusBarColor(Color.Transparent); 

2) Write these lines in all the possible combinations inside the style.xml (in 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>

Nothing seems to work, What should I do?

To hide the status bar and put the app fullscreen in your Android Manifest you need to set your application to a created Theme

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

To create a theme for API level before 21 in your Android Resources values folder add a styles.xml file. For example:

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

For Android API 21+ create another values folder values-v21 and add a styles.xml file like so:

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

Also, as far as I'm aware this should work for all Xamarin Forms Android apps API 19+ anything before that I haven't tried.

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

After so many research I got the best way from JoeManke's answer on xamarin forum.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM