操作栏背景不变

时间:2015-05-30 18:03:47

标签: java android

我已经开始使用android并按照他们的指示设置Action Bar的样式,但它无效。该应用程序一直停止当消息显示时,我可以看到更改的颜色。我有一个注意事项2和4.4.2 ..这是我的

样式。

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
    parent="@android:style/Theme.Holo.Light.DarkActionBar">
    <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">#F00</item>
</style>
</resources>`

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="funnestgames.actionbar" >

<application
    android:theme="@style/CustomActionBarTheme"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

和我的logcat。

    5-29 20:35:33.541      838-838/funnestgames.actionbar E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: funnestgames.actionbar, PID: 838
java.lang.RuntimeException: Unable to start activity     ComponentInfo{funnestgames.actionbar/funnestgames.actionbar.MainActivity}:     java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or     descendant) with this activity.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
        at android.app.ActivityThread.access$900(ActivityThread.java:175)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5602)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)

at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

at  android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:122)

at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
        at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:59)

at funnestgames.actionbar.MainActivity.onCreate(MainActivity.java:16)

at android.app.Activity.performCreate(Activity.java:5451)

at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
           
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
            
at android.app.ActivityThread.access$900(ActivityThread.java:175)
            
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
           
 at android.os.Handler.dispatchMessage(Handler.java:102)
            
at android.os.Looper.loop(Looper.java:146)
            
at android.app.ActivityThread.main(ActivityThread.java:5602)
            
at java.lang.reflect.Method.invokeNative(Native Method)
            
at java.lang.reflect.Method.invoke(Method.java:515)
            
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            
at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您可以按照mainActivity类

更改背景颜色
ActionBar actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#C71585")));

如果您对操作栏使用以下样式

,则不会出错
<style name="MyActionBar" parent="Theme.AppCompat.Light.DarkActionBar">