android appcompat工具栏样式与actionbar匹配

android appcompat工具栏样式与actionbar匹配,android,android-actionbar,styles,toolbar,Android,Android Actionbar,Styles,Toolbar,我正在使用android支持库工具栏,希望使用相同的背景颜色来匹配ActionBar 我查看了支持库源代码,找到了我想要使用的ActionBar样式: <style name="Base.Widget.AppCompat.Light.ActionBar.Solid"> <item name="background">?attr/colorPrimary</item> <item name="backgroundSt

我正在使用android支持库工具栏,希望使用相同的背景颜色来匹配ActionBar

我查看了支持库源代码,找到了我想要使用的ActionBar样式:

    <style name="Base.Widget.AppCompat.Light.ActionBar.Solid">
        <item name="background">?attr/colorPrimary</item>
        <item name="backgroundStacked">?attr/colorPrimary</item>
        <item name="backgroundSplit">?attr/colorPrimary</item>
    </style>
当我在工具栏布局中引用support library样式时,有人知道为什么它不起作用吗。我正在AndroidStudio中使用appcompat 22.2.0。 我还尝试了以下方法,但它们也不起作用:

    android:theme="@style/Base.Widget.AppCompat.Light.ActionBar.Solid"
    app:theme="@style/Base.Widget.AppCompat.Light.ActionBar.Solid"

这是因为工具栏需要一个主题。首先使用appcompat父项在主题中定义颜色属性

<style name="MyTheme" parent="Theme.AppCompat"> 
    <item name="background">@color/colorPrimary</item>
    <item name="backgroundStacked">@color/colorPrimary</item>
    <item name="backgroundSplit">@color/colorPrimary</item>
</style>
另外,确保你有

xmlns:app="http://schemas.android.com/apk/res-auto"
IDE可能会要求您自动更正此错误

工具栏的最终外观将是

 <android.support.v7.widget.Toolbar
    android:id="@+id/actiontoolbar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    app:theme="@style/MyTheme"
    />

使用appcompat 22.1.0,您可以在Toolbar@Sheychan你说的主题是什么意思?主题和样式是相同的,唯一的区别是如何将它们应用于应用程序、活动或视图。我已经尝试过这些组合,但它们仍然不起作用。app:theme=“@style/Base.Widget.AppCompat.Light.ActionBar.Solid”android:theme=“@style/Base.Widget.AppCompat.Light.ActionBar.Solid”style=“@style/Base.Widget.AppCompat.Light.ActionBar.Solid”上述操作均无效。我正在将appcompat 22.2.0与AndroidStudio@Sheychan它不起作用。定义自己的主题或风格仍然不起作用。我已经试过了。不知怎的,这条线。。。永远不会习惯。正如我在问题中提到的,如果有一个android:prefix作为独立的或作为样式的一部分,它就可以工作?attr/COLOR主要工作。那么为什么要用“android:background”而不是“background”?我可以相信“android:background”在较新的API上使用,但在工具栏或ActionBar不存在的API上,不应该使用“background”吗?我确实看到自API 1以来,“background”属性可用。这是为什么?有人知道属性是否就是这样应用的吗?这可能会回答这个问题
app:theme="@style/MyTheme"
xmlns:app="http://schemas.android.com/apk/res-auto"
 <android.support.v7.widget.Toolbar
    android:id="@+id/actiontoolbar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    app:theme="@style/MyTheme"
    />
android:theme