【发布时间】:2013-08-19 18:00:58
【问题描述】:
我在我的项目中使用Android Navigation bar,
我想将操作栏中的顶部颜色更改为红色,我该怎么做?
我有这样的东西,
我想要这样的东西,
我怎样才能做到这一点?
【问题讨论】:
标签: android colors background android-actionbar navigation-drawer
我在我的项目中使用Android Navigation bar,
我想将操作栏中的顶部颜色更改为红色,我该怎么做?
我有这样的东西,
我想要这样的东西,
我怎样才能做到这一点?
【问题讨论】:
标签: android colors background android-actionbar navigation-drawer
仅适用于 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
【讨论】:
No resource found that matches the given name '@style/Widget.Holo.Light.ActionBar.Solid.Inverse'. 将其更改为 android:style/Widget.Holo.Light.ActionBar.Solid.Inverse 可以解决问题。谢谢。
您可以通过创建自定义样式来定义 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?
【讨论】:
setTheme(R.style.MyCustomTheme)。
另一种可能性。
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));
【讨论】:
在 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>
【讨论】:
您可以通过这种方式更改操作栏颜色:
<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 以了解有关自定义调色板的更多信息
【讨论】:
一般而言,Android 操作系统利用“主题”来允许应用开发人员将一组通用的 UI 元素样式参数全局应用到整个 Android 应用,或者应用到单个 Activity 子类。
因此,有 3 个主流的 Android 操作系统“系统主题”,您可以在开发 3.0 版及更高版本的应用程序时在您的 Android Manifest XML 文件中指定它们
我在这里指的是 (APPCOMPAT) 支持库:-- 所以这三个主题是 1. AppCompat Light Theme(Theme.AppCompat.Light)
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 样式——
改变标题栏文字颜色(例如黑变白):--
现在要更改标题文本颜色,我们需要覆盖父引用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
【讨论】:
当我使用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
【讨论】:
使用这个 - http://jgilfelt.github.io/android-actionbarstylegenerator/
在几分钟内通过实时预览自定义您的操作栏的好工具。
【讨论】:
自定义颜色:
<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>
【讨论】:
我可以使用 titleTextColor 更改 ActionBar 文本颜色
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="titleTextColor">#333333</item>
</style>
【讨论】: