无法手动自定义操作栏

时间:2015-05-12 09:04:37

标签: android android-actionbar

IDE使用= Eclipse JUNO
API级别= 14
设备的操作系统= 4.2.2果冻豆

我一直在尝试使用XML文件更改操作栏的背景颜色和文本字体以及字体颜色。 我提到this教程并尝试了但是我收到了一条消息“不幸的是,MyAppName已被停止”

我用来改变背景的代码是:

RES /值/的themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">#6845db</item>
    </style>
</resources>

之后,我在Manifest中将我的应用主题更改为: -

android:theme="@style/MyTheme"

我是Android开发的初学者。

<小时/>
我的错误日志cat是: -

05-12 14:48:33.866: E/hawaii.gralloc(1449): GRALLOC ALLOC flags 933
05-12 14:48:33.886: E/HAWAII_EGL(1449): abuffer = (480 800) (933)
05-12 14:48:34.257: E/AndroidRuntime(7127): FATAL EXCEPTION: main
05-12 14:48:34.257: E/AndroidRuntime(7127): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.decode.timer/com.decode.timer.HomeScreen}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
05-12 14:48:34.257: E/AndroidRuntime(7127):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
05-12 14:48:34.257: E/AndroidRuntime(7127):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)
05-12 14:48:34.257: E/AndroidRuntime(7127):     at android.app.ActivityThread.access$700(ActivityThread.java:157)
05-12 14:48:34.257: E/AndroidRuntime(7127):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
05-12 14:48:34.257: E/AndroidRuntime(7127):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 14:48:34.257: E/AndroidRuntime(7127):     at android.os.Looper.loop(Looper.java:176)
05-12 14:48:34.257: E/AndroidRuntime(7127):     at android.app.ActivityThread.main(ActivityThread.java:5317)
05-12 14:48:34.257: E/AndroidRuntime(7127):     at java.lang.reflect.Method.invokeNative(Native Method)
05-12 14:48:34.257: E/AndroidRuntime(7127):     at java.lang.reflect.Method.invoke(Method.java:511)
05-12 14:48:34.257: E/AndroidRuntime(7127):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
05-12 14:48:34.257: E/AndroidRuntime(7127):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
05-12 14:48:34.257: E/AndroidRuntime(7127):     at dalvik.system.NativeStart.main(Native Method)
05-12 14:48:34.257: E/AndroidRuntime(7127): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
05-12 14:48:34.257: E/AndroidRuntime(7127):     at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:110)
05-12 14:48:34.257: E/AndroidRuntime(7127):     at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
05-12 14:48:34.257: E/AndroidRuntime(7127):     at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:99)
05-12 14:48:34.257: E/AndroidRuntime(7127):     at com.decode.timer.HomeScreen.onCreate(HomeScreen.java:15)
05-12 14:48:34.257: E/AndroidRuntime(7127):     at android.app.Activity.performCreate(Activity.java:5326)
05-12 14:48:34.257: E/AndroidRuntime(7127):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
05-12 14:48:34.257: E/AndroidRuntime(7127):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
05-12 14:48:34.257: E/AndroidRuntime(7127):     ... 11 more
05-12 14:48:34.657: E/hawaii.gralloc(1449): GRALLOC ALLOC flags 933
05-12 14:48:34.687: E/HAWAII_EGL(1449): abuffer = (480 235) (933)
05-12 14:48:34.867: E/android.os.Debug(1961): !@Dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error
05-12 14:48:35.087: E/hawaii.gralloc(1449): GRALLOC ALLOC flags 10000b22
05-12 14:48:35.147: E/hawaii.gralloc(1449): GRALLOC ALLOC flags 10000b22
05-12 14:48:35.157: E/HAWAII_EGL(1449): abuffer = (480 800) (10000b22)
05-12 14:48:35.618: E/hawaii.gralloc(1449): GRALLOC ALLOC flags 933
05-12 14:48:35.638: E/HAWAII_EGL(1449): abuffer = (480 235) (933)
05-12 14:48:35.848: E/hawaii.gralloc(1449): GRALLOC ALLOC flags 933
05-12 14:48:35.858: E/HAWAII_EGL(1449): abuffer = (480 235) (933)
05-12 14:48:45.688: E/dalvikvm(7141): adjustAdaptiveCoef max=4194304, min=1048576, ut=568

2 个答案:

答案 0 :(得分:1)

您的LogCat可能刚给您答案

  

05-12 14:48:34.257:E / AndroidRuntime(7127):引起:   java.lang.IllegalStateException:您需要使用Theme.AppCompat   这个活动的主题(或后代)。

所以你只需要像这样更改主题的父级

<style name="MyTheme" parent="@style/Theme.AppCompat.Light">
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">
    <item name="background">#6845db</item>
</style>

此外,此问题here可能有助于您了解在使用AppCompat时您不需要在项目名称前添加android:的原因

对于TitleTextAppearance,您必须创建一个样式,TextAppearance.AppCompat.Widget.ActionBar.Title作为父

<style name="MyTitleTextAppearance"
    parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="textColor">@color/white</item>
</style>

然后将其设置为titleTextStyle样式

中的MyActionBar
<item name="titleTextStyle">@style/MyTitleTextAppearance</item>

答案 1 :(得分:1)

你可以通过programmitacally来做到这一点..

final ActionBar abar = getSupportActionBar();
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#FE642E"));    
abar.setBackgroundDrawable(colorDrawable);

 abar.setDisplayShowCustomEnabled(true);
 abar.setDisplayShowTitleEnabled(false);
 abar.setDisplayHomeAsUpEnabled(true);
 abar.setHomeButtonEnabled(true);