Xamarin.Forms-更改StatusBar颜色 [英] Xamarin.Forms - Change StatusBar Color

查看:516
本文介绍了Xamarin.Forms-更改StatusBar颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了搜索,但无法从我的可移植代码中找到是否可以更改每个平台的StatusBar颜色? (对于Android, iOS & WinPhone 8.1)

I search but I can't find if it's possible to change the StatusBar color for each platform, from my portable code? (for Android, iOS & WinPhone 8.1)

public App()
{
    // Change the StatusBar color
    MainPage = new MainPageUser();
}

推荐答案

多年后,我将重新解决此问题,因为即使我的答案被接受为正确答案,但我的答案仍然是错误的.我现在已经解决了.

I'm coming back to this answer years later to fix it because my answer had been wrong even though it had been accepted as the correct answer. I have now fixed it.

我误读了要更改导航栏的问题,或者当时它在Android中的工作方式有所不同.

I had misread the question to want to change the navigation bar or that it worked differently in Android at that time.

我认为至少这是一个更好的答案,应该更好地帮助更改Android和iOS中导航栏的颜色.

I think at least this is a much better answer and should be better help to change the color of the navigationbar in Android and iOS.

将此代码添加到您的Xamarin.Forms项目

Add this code to your Xamarin.Forms project

public interface IStatusBarPlatformSpecific
{
  void SetStatusBarColor(Color color);
}

将此课程添加到您的Android项目

add this class to your Android project

[assembly: Dependency(typeof(MyDemo.Droid.CustomRenderers.Statusbar))]
namespace MyDemo.Droid.CustomRenderers
{
    public class Statusbar : IStatusBarPlatformSpecific
    {
       public Statusbar()
       {
       }

       public void SetStatusBarColor(Color color)
       {
         // The SetStatusBarcolor is new since API 21
         if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
        {
          var androidColor = color.AddLuminosity(-0.1).ToAndroid();
         //Just use the plugin
 CrossCurrentActivity.Current.Activity.Window.SetStatusBarColor(androidColor);
         }
         else
         {
          // Here you will just have to set your 
          // color in styles.xml file as shown below.
         }
       }
   }
}

将此 CurrentActivityPlugin 添加到您的项目

现在您可以像这样在Forms项目中更改颜色

Now you can change the color in your Forms project like this

var statusbar = DependencyService.Get<IStatusBarPlatformSpecific>();
statusbar.SetStatusBarColor(Color.Green);

注意上面的else语句.如果您使用的版本号低于21,则需要像这样将颜色硬编码到Android项目中的styles.xml中.

Note the else statement above. If you are using an older buildversion than 21 you will need to hard-code your color to the styles.xml in your Android project like this

<?xml version="1.0" encoding="utf-8" ?>
<resources>
   <style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">  
      <item name="android:statusBarColor">#544054</item>  
   </style>
</resources>

与iOS相似

将此添加到您的Info.plist(更多此处的文档)

add this to your Info.plist (more docs here)

<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleLightContent</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>

并将此代码添加到StatusBar类的iOS版本中的SetStatusBarColor方法中

and add this code to your SetStatusBarColor ethod in the iOS version of the StatusBar class

UIView statusBar = UIApplication.SharedApplication.ValueForKey(
new NSString("statusBar")) as UIView;

 if (statusBar != null && statusBar.RespondsToSelector(
 new  ObjCRuntime.Selector("setBackgroundColor:")))
 {
   // change to your desired color 
   statusBar.BackgroundColor = Color.FromHex("#7f6550").ToUIColor(); 
 }

我在如何设置颜色的基础上写了更详细的博客文章. Xamarin.Forms 中的状态栏(如果有人感兴趣的话).它只谈论Android和iOS,但应该让您知道如何使用其他平台.

I wrote a more detailed blog post on how to set the color of the statusbar from Xamarin.Forms if somebody is interested. It does only talk about Android and iOS but should give you an idea what to do with other platforms.

这篇关于Xamarin.Forms-更改StatusBar颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆