2012-05-10 118 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"))); 

它改变了背景颜色。希望能帮助到你。

相关问题