Xamarin:在导航过程中更改StatusBarColor

时间:2018-11-27 12:57:07

标签: xamarin statusbar custom-renderer

在MainActivity OnCreate中,我使用以下方法设置StatusBar的颜色:

Window.SetStatusBarColor(Resources.GetColor(Resource.Color.colorPrimary));

对于特定页面,我需要将StatusBar颜色设置为透明。

是否可以在Android自定义呈现类中做到这一点?

编辑: 我在自定义ANdorid上的OnLayout方法

 protected override void OnLayout(bool changed, int l, int t, int r, int b)
        {

            CustomNavigation.IgnoreLayoutChange = true;
            base.OnLayout(changed, l, t, r, b);
            CustomNavigation.IgnoreLayoutChange = false;

            int containerHeight = b - t;

            PageController.ContainerArea = new Rectangle(0, 0, Context.FromPixels(r - l), Context.FromPixels(containerHeight));


            if (Element?.Navigation?.NavigationStack.Count == 1)
            {
                CustomNavigation.BarBackgroundColor = Color.Transparent;
                //HERE I NEED TO HAVE STATUS AR TRANSPARENT 
            }


            if (Element?.Navigation?.NavigationStack.Count > 1)
            {
                PageController.ContainerArea = new Rectangle(0, 60, Context.FromPixels(r - l), Context.FromPixels(containerHeight));
                CustomNavigation.BarBackgroundColor = Color.FromHex("#006CA6");
            }


            for (var i = 0; i < ChildCount; i++)
            {
                AView child = GetChildAt(i);

                if (child is Android.Support.V7.Widget.Toolbar)
                {

                   continue;
                }

                child.Layout(0, 0, r, b);
            }
        }

1 个答案:

答案 0 :(得分:0)

状态栏的外观与其背景和文本颜色有关。这两个属性在不同的平台上都有其自身的局限性,但是,我们可以使用下面描述的解决方案来操纵这两个属性。

我们的目标很简单,我们希望能够在运行时在LightTheme和DarkTheme之间切换状态栏外观:

  • 在共享代码中定义接口:

     public interface IStatusBarStyleManager
     {
          void SetLightTheme();
          void SetDarkTheme();
     }
    

由于Android Lollipop(21)可以通过简单地使用keyColorPrimaryDark或通过编程在style.xml中定义它来设置自定义状态栏背景色,自Android M(23)起,可以设置预定义的状态栏文字颜色主题为浅色或深色。

Android代码:

   public class StatusBarStyleManager : IStatusBarStyleManager
{
public void SetDarkTheme()
{
    if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
    {
        Device.BeginInvokeOnMainThread(() =>
        {
            var currentWindow = GetCurrentWindow();
            currentWindow.DecorView.SystemUiVisibility = 0;
            currentWindow.SetStatusBarColor(Android.Graphics.Color.DarkCyan);
        });
    }
}

public void SetLightTheme()
{
    if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
    {
        Device.BeginInvokeOnMainThread(() =>
        {
            var currentWindow = GetCurrentWindow();
            currentWindow.DecorView.SystemUiVisibility = (StatusBarVisibility)SystemUiFlags.LightStatusBar;
            currentWindow.SetStatusBarColor(Android.Graphics.Color.LightGreen);
        });
    }
}

Window GetCurrentWindow()
{
    var window = CrossCurrentActivity.Current.Activity.Window;

    // clear FLAG_TRANSLUCENT_STATUS flag:
    window.ClearFlags(WindowManagerFlags.TranslucentStatus);

    // add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
    window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);

    return window;
}
}

我正在使用James Montemagno的Current Activity Plugin来获取当前活动的参考。

iOS代码:

在iOS中,状态栏的背景色默认情况下与导航栏的颜色匹配。换句话说,如果我们希望状态栏与导航栏的背景色相匹配,则不必显式设置状态栏的背景色。从iOS 7开始,可以将预定义的状态栏文本颜色主题设置为浅色或深色。但是,我们将不得不操纵Info.plist。由于状态栏的行为默认情况下是由视图控制器决定的,因此我们必须禁用此功能:

<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>

接下来,我们可以定义默认的文本颜色主题:

<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleDefault</string>


public class StatusBarStyleManager : IStatusBarStyleManager
{
    public void SetDarkTheme()
   {
        Device.BeginInvokeOnMainThread(() =>
       {
        UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.LightContent, false);
        GetCurrentViewController().SetNeedsStatusBarAppearanceUpdate();
    });
}

public void SetLightTheme()
{
    Device.BeginInvokeOnMainThread(() =>
    {
        UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.Default, false);
        GetCurrentViewController().SetNeedsStatusBarAppearanceUpdate();
    });
}

    UIViewController GetCurrentViewController()
   {
      var window = UIApplication.SharedApplication.KeyWindow;
      var vc = window.RootViewController;
      while (vc.PresentedViewController != null)
          vc = vc.PresentedViewController;
      return vc;
   }
 }

好运

在查询的情况下还原。