无法手动自定义操作栏
Not able to customize the actionbar manually
IDE 使用=Eclipse JUNO
API 等级=14
设备的 OS=4.2.2 Jelly Bean
我一直在尝试使用 XML 文件更改操作栏的背景颜色和文本字体以及字体颜色。
我参考了 this 教程并进行了尝试,但我收到一条消息 "Unfortunately, MyAppName has been stopped"
我用来更改背景的代码是:
res/values/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 开发的初学者。
我的错误日志猫是:-
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.access0(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
您的 LogCat 可能刚刚给了您答案
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.
所以你只需要像这样更改主题的父级
<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>
你可以通过编程来做到这一点..
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);
IDE 使用=Eclipse JUNO
API 等级=14
设备的 OS=4.2.2 Jelly Bean
我一直在尝试使用 XML 文件更改操作栏的背景颜色和文本字体以及字体颜色。 我参考了 this 教程并进行了尝试,但我收到一条消息 "Unfortunately, MyAppName has been stopped"
我用来更改背景的代码是:
res/values/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 开发的初学者。
我的错误日志猫是:-
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.access0(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
您的 LogCat 可能刚刚给了您答案
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.
所以你只需要像这样更改主题的父级
<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>
你可以通过编程来做到这一点..
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);