Android 自定义ActionBar主题不起作用

Android 自定义ActionBar主题不起作用,android,android-actionbar,android-theme,Android,Android Actionbar,Android Theme,我试图定制一个动作条的外观和感觉,但它似乎不起作用。当我特别将背景设置为红色时,背景显示为灰色 我的身材。格拉德尔 apply plugin: 'com.android.application' android { compileSdkVersion 22 buildToolsVersion "22.0.1" defaultConfig { applicationId "com.eliddell.rapsheet" minSdkVers

我试图定制一个动作条的外观和感觉,但它似乎不起作用。当我特别将背景设置为红色时,背景显示为灰色

我的身材。格拉德尔

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.eliddell.rapsheet"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    wearApp project(':wear')
    compile 'com.android.support:appcompat-v7:22.0.0'
    compile 'com.google.android.gms:play-services:7.0.0'
}
styles.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- Customize your theme here. -->
    </style>
    <style name="CustomActionBarTheme"
        parent="@style/AppTheme">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>
    <!-- ActionBar styles -->
    <style name="MyActionBar"
        parent="@style/CustomActionBarTheme">
        <item name="android:background">@color/red</item>
    </style>
</resources>
如果我将样式更新为:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="android:Theme.Holo.Light">
        <!-- Customize your theme here. -->
    </style>

    <style name="CustomActionBarTheme"
        parent="@style/AppTheme">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
        parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@color/red</item>
    </style>

</resources>

@样式/MyActionBar
@颜色/红色
然后我短暂地看到颜色变化,但应用程序崩溃:

原因:java.lang.IllegalStateException:您需要使用 Theme.AppCompat主题(或子代)与此活动


在SDK版本11之后使用ActionBar,获得ActionBar的实例,然后做任何你想做的事

在android清单中使用主题,如下所示:

android:theme="@style/AppTheme"
在活动中,它扩展为

public class MainActivity extends ActionBarActivity{
将操作栏的自定义视图充气为:

// Inflate your custom layout
        final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(
                R.layout.actionbar_layout,
                null);
        //Set Custom ActionBar
        actionBar = getSupportActionBar();
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setCustomView(actionBarLayout);

其中actionbar\u layout是actionbar的一个布局xml

好吧,我想出来了,谢谢大家的帮助。问题是我的样式xml指向“android:background”和“android:actionBarStyle”,但在支持库中,“android:”被删除

<resources>

<!-- Base application theme. -->
<style name="CustomActionBarTheme" parent="Theme.AppCompat.Light">
    <!-- Customize your theme here. -->
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>



<!-- ActionBar styles -->
<style name="MyActionBar" parent="Widget.AppCompat.ActionBar">
    <item name="android:windowBackground">@color/sapient_heat</item>

    <item name="background">@color/red</item>

</style>

@样式/MyActionBar
@颜色/热量
@颜色/红色

你是说不要使用主题吗?你在android清单中使用过AppTheme吗将此代码放在onCreate()ActionBar ActionBar=getSupportActionBar()中;使用此样式@android:color/black actionbaractivity无法解析。。我不理解您更改整个活动背景而不是操作栏的解决方案将自定义视图显示为:“//膨胀自定义布局最终视图组actionBarLayout=(ViewGroup)getLayoutInflater()。膨胀(R.layout.actionbar_layout,null);//设置自定义ActionBar ActionBar=getSupportActionBar();actionBar.setDisplayShowHomeEnabled(false);actionBar.setDisplayShowTitleEnabled(false);actionBar.setDisplayShowCustomEnabled(true);actionBar.setCustomView(actionBarLayout);'
public class MainActivity extends ActionBarActivity{
// Inflate your custom layout
        final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(
                R.layout.actionbar_layout,
                null);
        //Set Custom ActionBar
        actionBar = getSupportActionBar();
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setCustomView(actionBarLayout);
<resources>

<!-- Base application theme. -->
<style name="CustomActionBarTheme" parent="Theme.AppCompat.Light">
    <!-- Customize your theme here. -->
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>



<!-- ActionBar styles -->
<style name="MyActionBar" parent="Widget.AppCompat.ActionBar">
    <item name="android:windowBackground">@color/sapient_heat</item>

    <item name="background">@color/red</item>

</style>