【问题标题】:Can't change color of status bar无法更改状态栏的颜色
【发布时间】:2017-02-11 22:38:12
【问题描述】:

我正在尝试为我的应用设置主题以遵循材料 Android 外观。我的项目是一个 Xamarin.Android 项目,它以 API 23 为目标,但最低 API 目标是 16。所以我必须使用 AppCompat 库在所有 API 上进行相同的设计。

我遵循了这些指南:Material Theme,Beautiful Material Design with the Android Support Design Library,Android Tips: Hello AppCompatActivity, Goodbye ActionBarActivity 并创建了这些文件:

  • 资源/值/styles.xml:

    <resources>
      <style name="MyTheme" parent="MyTheme.Base">
      </style>
      <style name="MyTheme.Base" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">#F44336</item>
        <item name="colorPrimaryDark">#D32F2F</item>
      <item name="colorAccent">#FF4081</item>
      </style>
    </resources>
    
  • 资源/values-v21/styles.xml

     <resources>
     <!--
        Base application theme for API 21+. This theme replaces
        MyTheme from resources/values/styles.xml on API 21+ devices.
     -->
    <style name="MyTheme" parent="MyTheme.Base">
      <item name="android:windowContentTransitions">true</item>
      <item name="android:windowAllowEnterTransitionOverlap">true</item>
      <item name="android:windowAllowReturnTransitionOverlap">true</item>
      <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
      <item name="android:windowSharedElementExitTransition">@android:transition/move</item>
    </style>
    </resources>
    
  • MainActivity.cs

    using Android.App;
    using Android.Widget;
    using Android.OS;
    using Android.Support.V7.App;
    
    namespace TestThemeApp
    {
        [Activity(Label = "TestThemeApp", MainLauncher = true, Icon = "@drawable/icon", Theme = "@style/MyTheme")]
        public class MainActivity : AppCompatActivity
        {
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
    
                // Set our view from the "main" layout resource
                SetContentView (Resource.Layout.Main);
            }
        }
    }
    

我只是想让工具栏变成红色,状态栏变成深红色,但状态栏颜色不会改变。这是它的样子:

我做错了什么?

【问题讨论】:

  • 您正在测试的设备是什么 API 级别?我相信这仅适用于 API 21+ 设备。在旧平台上,AppCompat 尽可能模拟颜色主题。目前,这仅限于为操作栏和一些小部件着色。 android-developers.blogspot.in/2014/10/…
  • 我在模拟器上运行 API 24

标签: android xamarin material-design android-appcompat


【解决方案1】:

以你的风格试试这个 (Resources/values-v21/styles.xml)

<item name="android:windowDrawsSystemBarBackgrounds">true</item>
> 
<item name="android:statusBarColor">@color/cobalt_light</item>

我个人更喜欢在活动的onCreate 方法中处理它:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.setStatusBarColor(getResources().getColor(R.color.gray_very_dark));
    window.setNavigationBarColor(getResources().getColor(R.color.gray_darker));
}

【讨论】:

    【解决方案2】:

    对于一个简单的解决方法,您可以将主颜色更改为状态栏所需的颜色,它会自动更改状态栏的颜色。

    注意:在您找到正确答案之前,这是一种解决方法。

    【讨论】:

      【解决方案3】:

      您必须在 MainActivity 顶部添加一个标志,并在最后设置栏颜色。

      public class MainActivity : AppCompatActivity
      {
          protected override void OnCreate(Bundle bundle)
          {
              // First check if the current SDK is >= 21. Else it will cause trouble
              if ((int)Build.VERSION.SdkInt >= 21)
                  Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
      
              base.OnCreate(bundle);
      
              // Set our view from the "main" layout resource
              SetContentView (Resource.Layout.Main);
      
              SetBarColor();
          }
      
          public void SetBarColor ()
          {
              if((int)Build.VERSION.SdkInt >= 21)
                  Window.SetStatusBarColor(Color.ParseColor("#FF0000"));
          }
      }
      

      【讨论】:

        【解决方案4】:

        使用以下代码 sn-p 更改状态栏颜色:

        public void setStatusBarColor(Activity context, int color) {
                    if (Build.VERSION.SDK_INT >= 21) {
                        Window window = context.getWindow();
                        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                        window.setStatusBarColor(ContextCompat.getColor(context,color));
                    }
                }
        

        【讨论】:

          【解决方案5】:

          我认为我已经根据其他答案找到了最优雅的解决方案:

          我唯一改变的是在OnCreate(Bundle bundle) 函数中添加了标志:

           protected override void OnCreate(Bundle bundle)
           {
                 if ((int)Build.VERSION.SdkInt >= (int)BuildVersionCodes.Lollipop)
                          Window.AddFlags(Android.Views.WindowManagerFlags.DrawsSystemBarBackgrounds);
          
                 base.OnCreate(bundle);
          
                 // Set our view from the "main" layout resource
                 SetContentView (Resource.Layout.Main);
           }  
          

          就是这样!其余的由样式文件完成,在旧版本的 android 上,状态栏不会改变。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-10-25
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多