Android 使用 AppCompat 更改操作栏的背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23155637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change Background color of the action bar using AppCompat
提问by Bombolo
I found some questions about this problem around the web. Unfortunately, everything i try so far, has been unsuccessful.
我在网上发现了一些关于这个问题的问题。不幸的是,到目前为止我尝试的一切都没有成功。
Has the title say, i need to change the background color of my action bar.
标题说,我需要更改我的操作栏的背景颜色。
The project have a min sdk of 9, and max sdk of 19.
该项目的最小 sdk 为 9,最大 sdk 为 19。
I have create in my res/values folder, an xml file:
我在 res/values 文件夹中创建了一个 xml 文件:
red_actionbar.xml
red_actionbar.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
<item name="actionBarStyle">@style/MyActionBar</item>
</style>
<style name="MyActionBar"
parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="background">@color/red</item>
</style>
</resources>
the colors.xml stored in res/values
存储在 res/values 中的 colors.xml
<resources>
<color name="red">#FF0000</color>
</resources>
and the part of the manifest where i change the theme
以及清单中我更改主题的部分
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/CustomActionBarTheme" >
But nothing changes. Where is the problem? The application accepts the code because if i change ths:
但什么都没有改变。问题出在哪儿?应用程序接受代码,因为如果我更改这些代码:
<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
to
到
<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar ">
it does change the theme of my app, so the problem is in the style but I don't know how to solve it.
它确实改变了我的应用程序的主题,所以问题出在样式上,但我不知道如何解决。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
<item name="android:actionBarStyle" tools:ignore="NewApi">@style/MyActionBar</item>
<item name="actionBarStyle">@style/MyActionBar</item>
</style>
<style name="MyActionBar"
parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background" tools:ignore="NewApi">@color/red</item>
<item name="background">@color/red</item>
</style>
</resources>
回答by Blackbelt
<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background" tools:ignore="NewApi">@color/red</item>
<item name="background">@color/red</item>
</style>
<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
<item name="android:actionBarStyle" tools:ignore="NewApi">@style/MyActionBar</item>
<item name="actionBarStyle">@style/MyActionBar</item>
</style>
you need both android:background
and background
items. The former is for newer versions of android that support ActionBar natively. The latter is for older android versions.
你需要android:background
和background
物品。前者适用于本机支持 ActionBar 的较新版本的 android。后者适用于较旧的 android 版本。
EDIT
编辑
instead of <resources>
use
而不是<resources>
使用
<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
From SDK 21
来自 SDK 21
to change Action Bar background color, add this to ActionBarTheme, and the two colours are to be different or it will not work (thanks to @Dre
and @lagoman
)
要更改操作栏背景颜色,将其添加到 ActionBarTheme 中,并且两个颜色要不同否则将不起作用(感谢@Dre
和@lagoman
)
<item name="colorPrimary">@color/my_awesome_red</item>
<item name="colorPrimaryDark">@color/my_awesome_darker_red</item>
回答by Zohra Khan
Try this:
尝试这个:
<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
<item name="android:actionBarStyle">@style/MyActionBar</item>
<item name="actionBarStyle">@style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">@color/red</item>
<item name="background">@color/red</item>
</style>
回答by Mukul Aggarwal
Try this.
尝试这个。
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary"> #6699FF </item>
</style>
You can change #6699FF
color according to your choice (or use a color resource).
您可以#6699FF
根据自己的选择更改颜色(或使用颜色资源)。
回答by heloisasim
try to use
尝试使用
parent="Theme.AppCompat.Light"
instead of
代替
parent="@style/Theme.AppCompat.Light"