【问题标题】:Change background color of ActionBarSherlock programmatically以编程方式更改 ActionBarSherlock 的背景颜色
【发布时间】:2012-05-10 18:20:37
【问题描述】:

我的表单上有一个 ActionBarSherlock。我在运行时读取样式信息。其中一个样式是 ActionBar 的背景颜色。如何在运行时更改它?颜色可以是任意 RGB 值。

【问题讨论】:

标签: android android-widget android-actionbar


【解决方案1】:

也许这有帮助:How to set title color in ActionBarSherlock? via style 或getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.ad_action_bar_gradient_bak)); 通过编程方式

有主题

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

【讨论】:

  • 您仍在指定本地可绘制资源。我需要为从 Web 服务呈现给我的任何颜色执行此操作。
  • 我看到了你的新编辑,但我不知道这有什么帮助。应用程序启动后,我调用 Web 服务并返回颜色。说它是#123456。如何将其应用于您的硬编码#FF74af3b?
【解决方案2】:

一种方式:

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

0xff123456 是您所需的 ARGB 整数。

【讨论】:

    【解决方案3】:

    我只是用了下面的代码

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

    它改变了背景颜色。希望,它会有所帮助。

    【讨论】:

      猜你喜欢
      • 2012-04-21
      • 1970-01-01
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      • 2012-09-28
      • 1970-01-01
      • 2019-04-22
      • 1970-01-01
      相关资源
      最近更新 更多