细节:

我正在扩展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的背景颜色?


当前回答

对于Nexus 4用户来说,这似乎会让颜色变成灰色。

ActionBar bar = getActionBar(); // or MainActivity.getInstance().getActionBar()
bar.setBackgroundDrawable(new ColorDrawable(0xff00DDED));
bar.setDisplayShowTitleEnabled(false);  // required to force redraw, without, gray color
bar.setDisplayShowTitleEnabled(true);

(所有的功劳都归功于这篇文章,但它被淹没在评论中,所以我想在这里展示它) https://stackoverflow.com/a/17198657/1022454

其他回答

试试这个

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

使用下面的代码为动作栏文本和动作栏背景提供不同的颜色,只需使用下面的主题在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>

对于Nexus 4用户来说,这似乎会让颜色变成灰色。

ActionBar bar = getActionBar(); // or MainActivity.getInstance().getActionBar()
bar.setBackgroundDrawable(new ColorDrawable(0xff00DDED));
bar.setDisplayShowTitleEnabled(false);  // required to force redraw, without, gray color
bar.setDisplayShowTitleEnabled(true);

(所有的功劳都归功于这篇文章,但它被淹没在评论中,所以我想在这里展示它) https://stackoverflow.com/a/17198657/1022454

试试一行代码:

getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.MainColor)));

根据文档-“你可以用ActionBar API控制操作栏的行为和可见性,这是在Android 3.0 (API级别11)中添加的。”

因此,ActionBar将不能工作于API级别10 (Android 2.3.3)的目标环境。

以防万一,如果你的目标是最低API级别11,你可以通过定义自定义样式来改变ActionBar的背景颜色,如下所示:

<resources>
    <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:background">ANY_HEX_COLOR_CODE</item>
    </style>
</resources>

设置“MyTheme”为应用程序/活动的主题。