2016-10-03 191 views
-1

我正在嘗試主題我的應用遵循材質Android外觀。我的項目是一個Xamarin.Android項目,其目標是API 23,但最低API目標是16.因此,我必須使用AppCompat庫在所有API上使用相同的設計。無法更改狀態欄的顏色

我按照這些指南:Material ThemeBeautiful Material Design with the Android Support Design LibraryAndroid 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> 
    
  • 資源/值-V21 /風格.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); 
         } 
        } 
    } 
    

我只是試圖使工具欄紅色,狀態欄中深紅色,但狀態欄的顏色不會改變。這是什麼樣子:

Status bar won't change

我做了什麼錯?

+1

什麼API級別是您正在測試的設備?我相信這隻適用於API 21+設備。在較早的平臺上,AppCompat會在可能的情況下模擬顏色主題。目前這僅限於着色動作欄和一些小部件。 http://android-developers.blogspot.in/2014/10/appcompat-v21-material-design-for-pre。html –

+0

我在模擬器上運行API 24 –

回答

-1

我想我已經找到基於其他的答案中最優雅的解決方案:

加入我唯一改變的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上,狀態欄不會改變。

0

在你的風格試試這個(資源/值-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)); 
} 
0

對於簡單的解決方法,您可以將主要顏色更改爲狀態欄所需的顏色,並自動更改狀態欄的顏色。

注:這是一個工作,直到你找到正確的答案。

0

您必須在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")); 
    } 
} 
0

使用下面的代碼片段更改狀態欄顏色:

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