【问题标题】:How to set custom ActionBar color / style?如何设置自定义 ActionBar 颜色/样式?
【发布时间】:2013-08-19 18:00:58
【问题描述】:

我在我的项目中使用Android Navigation bar, 我想将操作栏中的顶部颜色更改为红色,我该怎么做? 我有这样的东西,

我想要这样的东西,

我怎样才能做到这一点?

【问题讨论】:

    标签: android colors background android-actionbar navigation-drawer


    【解决方案1】:

    仅适用于 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>
    

    然后将您的主题应用到您的整个应用或单个活动:

    更多详情Documentaion

    【讨论】:

    • 这很奇怪:你给 android 3.0 和更高版本的样式(holo)比 2.1 和更高版本(Material Design / Appcompat)...
    • @DalvikVM 是的..Appcompat 适用于旧版本。无需将其用于更高版本。在这个答案的时候没有介绍材料设计。
    • 我收到一个错误:No resource found that matches the given name '@style/Widget.Holo.Light.ActionBar.Solid.Inverse'. 将其更改为 android:style/Widget.Holo.Light.ActionBar.Solid.Inverse 可以解决问题。谢谢。
    • @style/Theme.AppCompat.Light.DarkActionBar 在我的情况下无法解决(Android Studio) - 它不应该是 @android:style/Theme.AppCompat.Light.DarkActionBar - 即带有前置样式前的“android”?
    【解决方案2】:

    您可以通过创建自定义样式来定义 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
    

    取自这里:How do I change the background color of the ActionBar of an ActionBarActivity using XML?

    【讨论】:

    • “然后将“MyCustomTheme”设置为包含 ActionBar 的 Activity 的主题是什么意思。”我该怎么做?
    • 在您的活动 XML 中,您必须将 android:theme="@style/MyCustomTheme" 作为参数添加到 标签
    • android 中剩下的 valuesv14 和 valuesv21 styles.xml 文件怎么样
    • 如何更改标题 textStyle?
    • @KalaJ 在您的活动的 onCreate 方法中使用 setTheme(R.style.MyCustomTheme)
    【解决方案3】:

    另一种可能性。

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        您可以通过这种方式更改操作栏颜色:

        <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 网站截取的屏幕截图,以使其更清晰,这是link 以了解有关自定义调色板的更多信息

        【讨论】:

          【解决方案6】:

          一般而言,Android 操作系统利用“主题”来允许应用开发人员将一组通用的 UI 元素样式参数全局应用到整个 Android 应用,或者应用到单个 Activity 子类。

          因此,有 3 个主流的 Android 操作系统“系统主题”,您可以在开发 3.0 版及更高版本的应用程序时在您的 Android Manifest XML 文件中指定它们

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

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

          1. 以及这两者的混合体,AppCompat Light Theme with the Darker ActionBar.(Theme.AppCompat.Light.DarkActionBar)

          AndroidManifest.xml 并查看标签,android 主题被提及为:-- android:theme="@style/AppTheme"

          打开 Styles.xml,我们在其中声明了基本应用程序主题:--

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

          我们需要覆盖这些父主题元素来设置操作栏的样式。

          不同颜色背景的ActionBar:--

          为此,我们需要创建一个新样式 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

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

          现在要更改标题文本颜色,我们需要覆盖父引用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>
          

          我们将在 MyActionBar 样式定义 中引用此样式定义,因为 TitleTextStyle 修改是 ActionBar 父 OS UI 元素的子元素。所以 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 选项菜单样式,请参阅this link

          【讨论】:

          • 嘿伙计,它成功了。一个疑问,如果我想改变菜单溢出图标的颜色,它会根据我们的主题改变颜色吗?深色主题的白色图标和浅色主题的深色图标,如何将其更改为白色?考虑答案中的最后一张图片。
          【解决方案7】:

          当我使用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

          【讨论】:

            【解决方案8】:

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

            在几分钟内通过实时预览自定义您的操作栏的好工具。

            【讨论】:

            • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
            【解决方案9】:

            自定义颜色:

             <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

            【讨论】:

              【解决方案10】:

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

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

              【讨论】:

                猜你喜欢
                • 2015-01-14
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2020-12-14
                • 2020-04-21
                相关资源
                最近更新 更多