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

问题描述 投票:0回答:20

详情:

我正在扩展 ActionBarActivity。
截至 2011 年 11 月 6 日,Eclipse 和 SDK 已完全修补。

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

部署到 Android 2.3.3 三星设备
申请有

android:theme="@android:style/Theme.Light"

问题:应用程序很轻,但是ActionBar是蓝色的,灰色的图标,在蓝色背景色的映衬下几乎看不出来。我还希望 ActionBar 变亮,这样它们的灰色图标就更明显了。

我尝试修改样式但无济于事
我可能遗漏了一些微不足道的东西。

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

android android-actionbar android-theme android-styles
20个回答
539
投票

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

因此,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”设置为应用程序/活动的主题。


219
投票

试试这个

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

127
投票

试试这个:

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

69
投票

使用这个 - http://jgilfelt.github.io/android-actionbarstylegenerator/

这是一个了不起的工具,可让您通过实时预览自定义操作栏。

我尝试了较早的答案,但在更改选项卡和字母等其他颜色时总是遇到问题,并且花了数小时来调整内容。这个工具帮助我在几分钟内完成了设计。

Here's a screenshot of the tool

一切顺利! :)


58
投票

我遇到了同样的问题,当我输入该代码时,我的操作栏会变成灰色。您的原始样式表很可能是这样的:

<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <!-- API 14 theme customizations can go here. -->
</style>

“DarkActionBar”使您的 Action Bar 保持灰色。我把它改成了这个,它起作用了:

<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
    <!-- API 14 theme customizations can go here. -->
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">#2aa4cd</item>
    <item name="android:titleTextStyle">@style/Theme.MyAppTheme.ActionBar.TitleTextStyle</item>
</style>        

<style name="Theme.MyAppTheme.ActionBar.TitleTextStyle" parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">#FFFFFF</item>
</style>    

我还投入了如何编辑文本颜色。此外,无需更改资源周围的任何内容。

-沃伦


39
投票

Actionbar 的行为也可以在 API 中更改< 11

参考Android官方文档

我正在使用

minSdkVersion = "9"
targetSdkVersion = "21"
构建一个应用程序我更改了操作栏的颜色并且它 适用于 API 级别 9

这是一个xml

res/values/themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
           parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>

        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
           parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@color/actionbar_background</item>

        <!-- Support library compatibility -->
        <item name="background">@color/actionbar_background</item>
    </style>
</resources>

并设置你想要的操作栏的颜色

res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="actionbar_background">#fff</color> //write the color you want here
</resources>

Actionbar颜色也可以在

.class
文件中定义,代码片段是

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

但这 不适用于API ,因此在 < 11xml

 中设置操作栏样式是 API 
的唯一方式 < 11


21
投票
对于 Kotlin 用户 - 我试过了,它适用于亮模式和暗模式 -

getSupportActionBar()?.setBackgroundDrawable( ColorDrawable(Color.parseColor("#003459")) )
像这样在onCreate中添加这个-

override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) getSupportActionBar()?.setBackgroundDrawable( ColorDrawable(Color.parseColor("#003459")) )
    

16
投票
在 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


14
投票
尝试一行代码:

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

9
投票
2021:没有弃用的 Kotlin oneliner:

supportActionBar?.setBackgroundDrawable(ColorDrawable(ContextCompat.getColor(this,R.color.red)))
只需将其放入onCreate并根据您的需要更改颜色


6
投票
这就是您可以更改操作栏颜色的方法。

import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.os.Build; import android.support.v4.content.ContextCompat; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; public class ActivityUtils { public static void setActionBarColor(AppCompatActivity appCompatActivity, int colorId){ ActionBar actionBar = appCompatActivity.getSupportActionBar(); ColorDrawable colorDrawable = new ColorDrawable(getColor(appCompatActivity, colorId)); actionBar.setBackgroundDrawable(colorDrawable); } public static final int getColor(Context context, int id) { final int version = Build.VERSION.SDK_INT; if (version >= 23) { return ContextCompat.getColor(context, id); } else { return context.getResources().getColor(id); } } }

从您的 MainActivity.java 中像这样更改操作栏颜色

ActivityUtils.setActionBarColor(this, R.color.green_00c1c1);
    

5
投票
当您扩展活动时使用以下代码

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

当您扩展 AppCompatActivity 使用以下代码

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

4
投票
有时这个选项会抛出 NullPointerException

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



但是这个选项对我有用。所以你可以试试这个。

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

快乐编码


3
投票
这对我有用,对于 AppCompatActivity,

<item name="actionBarStyle">@style/BlackActionBar</item> </style> <style name="BlackActionBar" parent="@style/Widget.AppCompat.ActionBar.Solid"> <item name="background">@android:color/black</item> <item name="titleTextStyle">@style/BlackActionBarTitleTextStyle</item> </style> <style name="BlackActionBarTitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"> <item name="android:textColor">@android:color/white</item> <item name="android:fontFamily">@font/cinzel_decorative</item> </style>
    

3
投票
如果你使用的是 androidx AppCompact。使用下面的代码。

androidx.appcompat.app.ActionBar actionBar = getSupportActionBar(); actionBar.setBackgroundDrawable(new ColorDrawable("Color"));
    

2
投票
此代码可能有帮助

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


2
投票
对于那些使用 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>

    

1
投票
使用此代码 .. 更改操作栏背景颜色。 打开“res/values/themes.xml”(如果不存在,创建它) 并添加

<?xml version="1.0" encoding="utf-8"?> <resources> <!-- the theme applied to the application or activity --> <style name="CustomActionBarTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar"> <item name="android:actionBarStyle">@style/MyActionBar</item> </style> <!-- ActionBar styles --> <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse"> <item name="android:background">@drawable/actionbar_background</item> </style>

注意:此代码仅适用于 android 3.0 及更高版本


0
投票
使用这个,它会起作用。

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

-2
投票
getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.TabColor)));

color.xml文件:

<resources> <color name="TabColor">#11FF00</color> </resources>`
    
© www.soinside.com 2019 - 2024. All rights reserved.