Android 使用AppCompat更改操作栏的背景色
我在网上发现了一些关于这个问题的问题。不幸的是,到目前为止,我尝试的一切都没有成功 标题说,我需要改变我的动作栏的背景色 该项目的最小sdk为9,最大sdk为19 我在我的res/values文件夹中创建了一个xml文件: red_actionbar.xmlAndroid 使用AppCompat更改操作栏的背景色,android,xml,android-actionbar,Android,Xml,Android Actionbar,我在网上发现了一些关于这个问题的问题。不幸的是,到目前为止,我尝试的一切都没有成功 标题说,我需要改变我的动作栏的背景色 该项目的最小sdk为9,最大sdk为19 我在我的res/values文件夹中创建了一个xml文件: red_actionbar.xml <?xml version="1.0" encoding="utf-8"?> <resources> <style name="CustomActionBarTheme" parent="@style/
<?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>
@样式/MyActionBar
@颜色/红色
存储在res/values中的colors.xml
<resources>
<color name="red">#FF0000</color>
</resources>
#FF0000
还有舱单上我改变主题的那部分
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/CustomActionBarTheme" >
但一切都没有改变。问题在哪里?应用程序接受代码,因为如果我更改代码:
<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
到
它确实改变了我应用程序的主题,所以问题在于风格,但我不知道如何解决它
<?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>
@样式/MyActionBar
@样式/MyActionBar
@颜色/红色
@颜色/红色
试试这个:
<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>
@样式/MyActionBar
@样式/MyActionBar
@颜色/红色
@颜色/红色
来自SDK 21
若要更改操作栏背景颜色,请将其添加到ActionBarTheme,两种颜色将不同,否则将无法使用(感谢@Dre
和@lagoman
)
@color/my\u awesome\u red
@颜色/我的超棒的深红色
尝试使用
parent="Theme.AppCompat.Light"
而不是
parent="@style/Theme.AppCompat.Light"
试试这个
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary"> #6699FF </item>
</style>
#6699FF
您可以根据自己的选择更改颜色(或使用颜色资源)。安卓的哪个版本有该设备,您在哪里测试代码?安卓4.2.2三星s2 Plus安装总是很棘手。这是一个很大的混乱和缺乏文件记录。另一方面,您是否需要至少支持9个Android SDK?您将涵盖绝大多数运行15或更高版本的设备。。。。只是说…我不能用。类型不匹配:无法从android.app.ActionBar转换为android.support.v7.app.ActionBar。我的活动扩展了ActionBarActivity以支持sdk 9。我还可以知道如何更改标题颜色吗?谢谢!我在这上面花了5个小时!!我不明白为什么要将编辑添加到接受的答案中??只需复制并粘贴到styles.xml??我运气不好:(如果我使用android:actionBarStyle,我会出错android:actionBarStyle需要API级别11(当前最小值为9)如果您已将SDK更新为API级别21,要更改操作栏背景颜色,请将此添加到
ActionBarTheme
:@color/my_awesome_red@color/my_awesome_dark_red这是根据以下说明的:需要注意的是,colorPrimary和colorPrimaryDark不应该是相同的颜色。如果是,Android将不会为操作栏着色。我可以我没有告诉你我倒了多少线,直到找到这条才奏效。非常感谢!似乎什么都不起作用。令人惊讶的是,你花了多少时间来做一件简单的事情,比如设置动作条的背景颜色……对我来说……太好了
<item name="colorPrimary">@color/my_awesome_red</item>
<item name="colorPrimaryDark">@color/my_awesome_darker_red</item>
parent="Theme.AppCompat.Light"
parent="@style/Theme.AppCompat.Light"
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary"> #6699FF </item>
</style>