Android导航栏
,我想将操作栏顶部的颜色改为红色,如何做到?
我有类似这样的东西,
Android导航栏
,我想将操作栏顶部的颜色改为红色,如何做到?
我有类似这样的东西,
您可以通过创建自定义样式来定义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
一般来说,Android操作系统利用“主题”机制,使应用程序开发人员可以全局应用一组通用的UI元素样式参数到整个Android应用程序中,或者选择应用于单个Activity子类。
因此,在为3.0版本及更高版本开发应用程序时,您可以在Android清单XML文件中指定三种主流的Android操作系统“系统主题”。
我这里提到的是(APPCOMPAT)支持库:-- 所以这三个主题是 1. AppCompat浅色主题 (Theme.AppCompat.Light)
打开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样式,如下所示--
更改标题栏文字颜色(例如从黑色到白色):--
现在要更改标题文本颜色,我们需要覆盖父引用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选项菜单样式,请参考此链接
仅适用于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>
然后将您的主题应用于整个应用程序或单独的活动:
了解更多详细信息,请参见文档
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 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网站截取的屏幕截图,以使内容更加清晰。这里是一个链接,可了解更多有关自定义色彩调色板的信息。
另一种可能性是使用以下代码更改ActionBar的背景颜色。
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));
我可以使用titleTextColor来更改ActionBar文本颜色。
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="titleTextColor">#333333</item>
</style>
自定义颜色:
<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
由于我在使用`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
<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>
使用这个工具 - http://jgilfelt.github.io/android-actionbarstylegenerator/
这是一个很好的工具,可以在几分钟内通过实时预览自定义你的操作栏。
setTheme(R.style.MyCustomTheme)
。 - NullByte08