2012-05-10 113 views
6

我的窗體上有一個ActionBarSherlock。我在運行時閱讀樣式信息。其中一種樣式是ActionBar的背景顏色。我如何在運行時改變它?顏色可以是任何RGB值。以編程方式更改ActionBarSherlock的背景顏色

+0

Look to this one http://stackoverflow.com/questions/10064411/change-actionbarsherlock-background-color – sonida

回答

8

也許這幫助:通過風格 或getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.ad_action_bar_gradient_bak));How to set title color in ActionBarSherlock?通過編程

隨着主題

// add theme in app 
<application android:theme="@style/MainTheme"></application> 

// MainTheme 
<style name="MainTheme" parent="Theme.Sherlock.Light.DarkActionBar">  
</style> 

// MainThemeGreen 
<style name="MainThemeGreen" parent="Theme.Sherlock.Light.DarkActionBar"> 
    <item name="android:actionBarStyle">@style/MainTheme.ActionBarStyle</item>   
</style> 

// ActionBar 
<style name="MainTheme.ActionBarStyle" parent="Widget.Sherlock.Light.ActionBar"> 
    <item name="android:background">@drawable/bg_green_actionbar</item> 
    <item name="android:titleTextStyle">@style/MainTheme.ActionBar.TitleTextStyle</item> 
</style> 

// Text style 
<style name="MainTheme.ActionBar.TitleTextStyle" parent="TextAppearance.Sherlock.Widget.ActionBar.Title"> 
    <item name="android:textColor">@color/White</item> 
</style> 

// bg_green_actionbar.xml 
<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item> 
     <shape> 
      <solid android:color="#ff74af3b" /> 
     </shape> 
    </item> 
</layer-list> 

在這之後,你可以在動態更改主題:setTheme(R.styles.MainThemeGreen);

+0

你還在指定一個本地可繪製資源。我需要爲網絡服務提供給我的任何顏色。 –

+0

我看到您的新編輯,但我沒有關注它如何幫助。應用程序啓動後,我會調用Web服務並返回一種顏色。說它是#123456。我如何將它應用於硬編碼的#FF74af3b? –

6

方式一:

mSupportActionBar = getSupportActionBar(); 
mSupportActionBar.setBackgroundDrawable(new ColorDrawable(0xff123456)); 

其中0xff123456是您所需的ARGB整數。

5

我只是用下面的代碼

getSupportActionBar().setBackgroundDrawable(new 
    ColorDrawable(Color.parseColor("#00853c"))); 

它改變了背景顏色。希望能幫助到你。

相關問題