细节:

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


当前回答

试试一行代码:

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

其他回答

试试这个

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

根据文档-“你可以用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”为应用程序/活动的主题。

用这个就行了。

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

有时这个选项会抛出NullPointerException

ActionBar = getActionBar(); actionbar。setBackgroundDrawable(新ColorDrawable(“颜色”));

但这个选择对我来说是可行的。你可以试试这个。

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

快乐的编码

这段代码可能会有帮助

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- Customize your theme here. -->
</style>
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- customize the color palette -->
    <item name="colorPrimary">@color/material_blue_500</item>
    <item name="colorPrimaryDark">@color/material_blue_700</item>
    <item name="colorAccent">@color/material_blue_500</item>
    <item name="colorControlNormal">@color/black</item>

</style>
<style name="CardViewStyle" parent="CardView.Light">
    <item name="android:state_pressed">@color/material_blue_700</item>
    <item name="android:state_focused">@color/material_blue_700</item>
    <!--<item name="android:background">?android:attr/selectableItemBackground</item>-->
</style>