Cannot truely hide the android statusbar working with Xamarin.Forms

六月ゝ 毕业季﹏ 提交于 2020-01-07 02:57:12

问题


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?


回答1:


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.




回答2:


If you use Navigation try NavigationPage.SetHasNavigationBar




回答3:


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


来源:https://stackoverflow.com/questions/40913652/cannot-truely-hide-the-android-statusbar-working-with-xamarin-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!