利用AppCompat操作栏的更改背景颜色 [英] Change Background color of the action bar using AppCompat

查看:319
本文介绍了利用AppCompat操作栏的更改背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现有关Web解决此问题的一些问题。不幸的是,一切都我尝试至今,一直未果。

的标题说,我需要改变我的操作栏的背景颜色。

该项目有最小的SDK 9,和最大的SDK 19。

我已创建在我的RES /值文件夹,一个XML文件:

red_actionbar.xml

 < XML版本=1.0编码=UTF-8&GT?;
<资源>
    <样式名称=CustomActionBarTheme父=@风格/ Theme.AppCompat.Light>

        <项目名称=actionBarStyle> @风格/ MyActionBar< /项目>

    < /风格>

    <样式名称=MyActionBar父=@风格/ Widget.AppCompat.Light.ActionBar.Solid.Inverse>


        <项目名称=背景> @色/红色和LT; /项目>

    < /风格>

< /资源>
 

存储资源的colors.xml /值

 <资源>
   <颜色名称=红>#FF0000< /彩色>
< /资源>
 

和清单的一部分,在那里我更改主题

 <应用
    机器人:allowBackup =真
    机器人:图标=@可绘制/ ic_launcher
    机器人:标签=@字符串/ APP_NAME
    机器人:主题=@风格/ CustomActionBarTheme>
 

但是,没有什么变化。

问题出在哪里?

应用程序接受code,因为如果我改变

 <样式名称=CustomActionBarTheme父=@风格/ Theme.AppCompat.Light>
 

 <样式名称=CustomActionBarTheme父=@风格/ Theme.AppCompat.Light.DarkActionBar>
 

它改变我的应用程序的主题,所以这个问题是在风格,但我不知道硬件来解决它。

 < XML版本=1.0编码=UTF-8&GT?;
<资源>
<样式名称=CustomActionBarTheme父=@风格/ Theme.AppCompat.Light>
    <项目名称=机器人:actionBarStyle工具:忽略=NewApi> @风格/ MyActionBar< /项目>
    <项目名称=actionBarStyle> @风格/ MyActionBar< /项目>

< /风格>

<样式名称=MyActionBar父=@风格/ Widget.AppCompat.Light.ActionBar.Solid.Inverse>
    <项目名称=机器人:背景工具:忽略=NewApi> @色/红色和LT; /项目>
    <项目名称=背景> @色/红色和LT; /项目>
< /风格>

< /资源>
 

解决方案

 <样式名称=MyActionBar父=@风格/ Widget.AppCompat.Light.ActionBar.Solid。反>
    <项目名称=机器人:背景工具:忽略=NewApi> @色/红色和LT; /项目>
    <项目名称=背景> @色/红色和LT; /项目>
< /风格>

<样式名称=CustomActionBarTheme父=@风格/ Theme.AppCompat.Light>
    <项目名称=机器人:actionBarStyle工具:忽略=NewApi> @风格/ MyActionBar< /项目>
    <项目名称=actionBarStyle> @风格/ MyActionBar< /项目>

< /风格>
 

您需要同时安卓背景背景项目。前者是针对Android的支持动作条本身较新版本。后者是对老年人的Andr​​oid版本。

修改

而不是<资源>

 <资源的xmlns:工具=htt​​p://schemas.android.com/tool​​s的xmlns:机器人=htt​​p://schemas.android.com/apk/res / Android的>
 

从SDK 21

要更改操作栏的背景颜色,将其添加到ActionBarTheme,而两种颜色是不同的,否则将无法正常工作(感谢 @Dre @lagoman

 <项目名称=colorPrimary> @色/ my_awesome_red< /项目>
<项目名称=colorPrimaryDark> @色/ my_awesome_darker_red< /项目>
 

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.

I have create in my res/values folder, an xml file:

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

<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 change.

Where is the problem?

The application accept the code because if i change

        <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">

to

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

it change the theme of my app, so the problem is in the style but i don't know hw 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>

解决方案

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

EDIT

instead of <resources> use

<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">

From 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)

<item name="colorPrimary">@color/my_awesome_red</item> 
<item name="colorPrimaryDark">@color/my_awesome_darker_red</item>

这篇关于利用AppCompat操作栏的更改背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆