简体   繁体   English

以编程方式更改ActionBarSherlock的背景颜色

[英]Change background color of ActionBarSherlock programmatically

I have an ActionBarSherlock on my form. 我的表单上有一个ActionBarSherlock。 I'm reading style information at runtime. 我正在运行时阅读样式信息。 One of the pieces of style is the background color of the ActionBar. 其中一个样式是ActionBar的背景颜色。 How can I change this at runtime? 如何在运行时更改此设置? The color can be any RGB value. 颜色可以是任何RGB值。

Maybe this help : How to set title color in ActionBarSherlock? 也许这个帮助: 如何在ActionBarSherlock中设置标题颜色? via style or getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.ad_action_bar_gradient_bak)); via style或getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.ad_action_bar_gradient_bak)); via programmatically 通过编程方式

With the theme 以主题为主题

// 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>

After this you can change Theme on fly: setTheme(R.styles.MainThemeGreen); 在此之后你可以在飞行中改变主题:setTheme(R.styles.MainThemeGreen);

One way: 单程:

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

where 0xff123456 is your required ARGB integer. 其中0xff123456是您所需的ARGB整数。

I just used below Code 我刚刚在Code下面使用过

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

it changed the bg color. 它改变了bg的颜色。 Hope, it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM