细节:

我正在扩展ActionBarActivity。 Eclipse和SDK已于2011-11-06完全补丁。

<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14" />  

部署到三星设备的Android 2.3.3 应用程序有android:theme="@android:style/ theme。光”

问题:应用程序很轻,但ActionBar是蓝色的灰色图标,在蓝色背景色下几乎看不见。我还想让动作栏变得更轻,这样它们的灰色图标就更明显了。

我试过修改样式,但没有用。 我可能遗漏了一些小事。

如何使用XML更改ActionBarActivity的ActionBar的背景颜色?


当前回答

当您扩展活动时,请使用以下代码

ActionBar actionbar = getActionBar();
actionbar.setBackgroundDrawable(new ColorDrawable("color"));

当你扩展AppCompatActivity时,使用下面的代码

ActionBar actionbar = getSupportActionBar();
actionbar.setBackgroundDrawable(new ColorDrawable("color"));

其他回答

对于那些使用API 21或更高版本的人来说,使用下面的代码非常简单

暗动作栏

<resources>
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="android:background">@color/colorDarkGrey</item>
</style>

for_LightActionBar

<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.ActionBar">
    <item name="android:background">@color/colorDarkGrey</item>
</style>

2021: Kotlin联机程序,没有弃用:

supportActionBar?.setBackgroundDrawable(ColorDrawable(ContextCompat.getColor(this,R.color.red)))

只需将其放入onCreate并根据需要更改颜色

试试这个:

ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));

使用下面的代码为动作栏文本和动作栏背景提供不同的颜色,只需使用下面的主题在manifest中针对您想要输出的活动:)

<style name="MyTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:titleTextStyle">@style/TitleBarTextColor</item>
    <item name="android:background">YOUR_COLOR_CODE</item>
</style>

<style name="TitleBarTextColor" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">YOUR_COLOR_CODE</item>
</style>

试试这个

ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable("COLOR"));