如何设置自定义的ActionBar颜色/样式?

93
我在我的项目中使用了 Android导航栏,我想将操作栏顶部的颜色改为红色,如何做到? 我有类似这样的东西,

top black

我希望得到下面这样的效果,

top red

怎么实现呢?
10个回答

182

您可以通过创建自定义样式来定义ActionBar(和其他内容)的颜色:

只需编辑Android项目的res/values/styles.xml文件即可。

例如,像这样:

<resources>
    <style name="MyCustomTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyActionBarTheme</item>
    </style>

    <style name="MyActionBarTheme" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">ANY_HEX_COLOR_CODE</item>
    </style>
</resources>

然后将"MyCustomTheme"设置为包含ActionBar的Activity的主题。

您也可以像这样为ActionBar设置颜色:

ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.RED)); // set your desired color

取自这里:如何使用XML更改ActionBarActivity的ActionBar的背景颜色?


2
“Then set "MyCustomTheme" as the Theme of your Activity that contains the ActionBar." 这句话是什么意思?我该怎么做? - Kala J
2
在您的活动XML中,您必须将*android:theme="@style/MyCustomTheme"*作为参数添加到<android.support.v4.view.ViewPager.../>标签内。 - PedroD
2
Android中的styles.xml文件中剩余的valuesv14和valuesv21样式呢? - Harsha
如何更改标题文本样式? - AdamHurwitz
在您的活动的onCreate方法中使用setTheme(R.style.MyCustomTheme) - NullByte08

65

一般来说,Android操作系统利用“主题”机制,使应用程序开发人员可以全局应用一组通用的UI元素样式参数到整个Android应用程序中,或者选择应用于单个Activity子类。

因此,在为3.0版本及更高版本开发应用程序时,您可以在Android清单XML文件中指定三种主流的Android操作系统“系统主题”。

我这里提到的是(APPCOMPAT)支持库:-- 所以这三个主题是 1. AppCompat浅色主题 (Theme.AppCompat.Light)

enter image description here

  1. AppCompat深色主题(Theme.AppCompat),

enter image description here

  1. 还有两者之间的混合体,带有较暗ActionBar的AppCompat浅色主题。 (Theme.AppCompat.Light.DarkActionBar)

打开AndroidManifest.xml并查看<application>标签,将android主题指定为:android:theme = "@style/AppTheme"

打开Styles.xml文件,我们在那里声明了基本应用程序主题:--

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
  </style>

我们需要覆盖这些父主题元素以样式化操作栏。

具有不同颜色背景的操作栏:

要做到这一点,我们需要创建一个名为MyActionBar(您可以随意命名)的新样式,并将其父引用设置为 @style/Widget.AppCompat.Light.ActionBar.Solid.Inverse,该引用保存了Android ActionBar UI元素的样式特征。因此定义如下:

<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="background">@color/red</item>
</style>

我们需要在我们的AppTheme中引用这个定义,并指向覆盖的ActionBar样式,如下所示--

@style/MyActionBar 具有粉色背景颜色的ActionBar

更改标题栏文字颜色(例如从黑色到白色):--

现在要更改标题文本颜色,我们需要覆盖父引用parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"

因此,样式定义将为:

<style name="MyActionBarTitle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/white</item>
</style>

由于TitleTextStyle修改是ActionBar父OS UI元素的子元素,因此我们将在 MyActionBar style definition中引用此样式定义。因此,MyActionBar样式元素的最终定义将为:

<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="background">@color/red</item>
    <item name="titleTextStyle">@style/MyActionBarTitle</item>
</style>

所以这是最终的Styles.xml文件。

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- This is the styling for action bar -->
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>

    <style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="background">@color/red</item>
        <item name="titleTextStyle">@style/MyActionBarTitle</item>
    </style>

    <style name="MyActionBarTitle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@color/white</item>
    </style>
</resources>

白色标题颜色的ActionBar 如需进一步定制ActionBar选项菜单样式,请参考此链接


2
嘿,伙计,它起作用了。有一个问题,如果我想改变菜单溢出图标的颜色,它会根据我们的主题来更改颜色对吧?在深色主题中使用白色图标,在浅色主题中使用黑色图标。我如何将其更改为白色?请考虑您回答中的最后一张图片。 - Ameen Maheen

33

仅适用于Android 3.0及以上版本

当仅支持Android 3.0及以上版本时,您可以按照以下方式定义操作栏的背景:

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.Holo.Light.DarkActionBar">
       <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

<!-- ActionBar styles -->
  <style name="MyActionBar" parent="@style/Widget.Holo.Light.ActionBar.Solid.Inverse">
       <item name="android:background">#ff0000</item>
  </style>
</resources>

适用于 Android 2.1 及更高版本

使用支持库时,您的样式 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">@drawable/actionbar_background</item>

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

然后将您的主题应用于整个应用程序或单独的活动:

了解更多详细信息,请参见文档


这很奇怪:你给 Android 3.0 及更高版本一个比 2.1 及更高版本(Material Design / Appcompat)更老的样式(Holo)... - Roel
@DalvikVM 是的..Appcompat是为旧版本设计的。在较高版本中不需要使用它。在回答这个问题的时候,Material Design还没有被引入。 - Ketan Ahir
我遇到了一个错误:No resource found that matches the given name '@style/Widget.Holo.Light.ActionBar.Solid.Inverse'。将其更改为android:style/Widget.Holo.Light.ActionBar.Solid.Inverse可以解决问题。谢谢。 - Annie Lagang
@style/Theme.AppCompat.Light.DarkActionBar 在我的情况下无法解析(Android Studio)- 它不应该是 @android:style/Theme.AppCompat.Light.DarkActionBar 吗?即在样式之前加上“android”? - AgentKnopf

32

你可以按照以下方式更改操作栏的颜色:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/green_action_bar</item>
</style>

这就是更改操作栏颜色所需的全部内容。

此外,如果你想要改变状态栏的颜色,只需要添加以下代码:

 <item name="android:colorPrimaryDark">@color/green_dark_action_bar</item>

这是从开发者Android网站截取的屏幕截图,以使内容更加清晰。这里是一个链接,可了解更多有关自定义色彩调色板的信息。

输入图像描述


7

另一种可能性是使用以下代码更改ActionBar的背景颜色。

actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));


5

我可以使用titleTextColor来更改ActionBar文本颜色。

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="titleTextColor">#333333</item>
</style>

4

自定义颜色:

 <style name="AppTheme" parent="Theme.AppCompat.Light">

                <item name="colorPrimary">@color/ColorPrimary</item>
                <item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
                <!-- Customize your theme here. -->
     </style>

自定义样式:

 <style name="Theme.AndroidDevelopers" parent="android:style/Theme.Holo.Light">
        <item name="android:selectableItemBackground">@drawable/ad_selectable_background</item>
        <item name="android:popupMenuStyle">@style/MyPopupMenu</item>
        <item name="android:dropDownListViewStyle">@style/MyDropDownListView</item>
        <item name="android:actionBarTabStyle">@style/MyActionBarTabStyle</item>
        <item name="android:actionDropDownStyle">@style/MyDropDownNav</item>
        <item name="android:listChoiceIndicatorMultiple">@drawable/ad_btn_check_holo_light</item>
        <item name="android:listChoiceIndicatorSingle">@drawable/ad_btn_radio_holo_light</item>
    </style>

更多信息请参见:Android ActionBar


3

由于我在使用`AppCompatActivity`时,以上解决方法对我无效。但是以下解决方法有效:

res/styles.xml中进行如下更改:

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
</style>


PS: 我已经使用了colorPrimary代替android:colorPrimary


1
在 style.xml 文件中添加以下代码。
<resources>
    <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyAction</item>
    </style>

    <style name="MyActionBarTheme" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">#333333</item>
    </style>
</resources>

0

1
虽然这个链接可能回答了问题,但最好在此处包含答案的基本部分并提供参考链接。仅有链接的答案如果链接页面发生更改可能会变得无效。 - Roman Marusyk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接