搜索很多,但沒有任何用處。在Android中自定義標題欄。獲得例外您需要使用Theme.AppCompat主題
我創造我的自定義標題欄一個簡單的應用程序,但不是從天變得那麼遠。我正在使用這個https://developer.android.com/training/basics/actionbar/styling.html。
的Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.title"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<application
android:theme="@style/CustomActionBarTheme"//title bar
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name="com.example.title.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>
我創建theme.xml像RES /價值/的themes.xml。
代碼:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/MyActionBar</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">@style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#CCCCCC</item>
<!-- Support library compatibility -->
</style>
</resources>
獲取異常:( logcat的:
04-22 11:37:21.755: E/test(11652): Exception
04-22 11:37:21.757: E/AndroidRuntime(11652): FATAL EXCEPTION: main
04-22 11:37:21.757: E/AndroidRuntime(11652): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.title/com.example.title.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.app.ActivityThread.access$600(ActivityThread.java:156)
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.os.Looper.loop(Looper.java:153)
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.app.ActivityThread.main(ActivityThread.java:5299)
04-22 11:37:21.757: E/AndroidRuntime(11652): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 11:37:21.757: E/AndroidRuntime(11652): at java.lang.reflect.Method.invoke(Method.java:511)
04-22 11:37:21.757: E/AndroidRuntime(11652): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
04-22 11:37:21.757: E/AndroidRuntime(11652): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
04-22 11:37:21.757: E/AndroidRuntime(11652): at dalvik.system.NativeStart.main(Native Method)
04-22 11:37:21.757: E/AndroidRuntime(11652): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:111)
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
04-22 11:37:21.757: E/AndroidRuntime(11652): at com.example.title.MainActivity.onCreate(MainActivity.java:18)
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.app.Activity.performCreate(Activity.java:5182)
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
04-22 11:37:21.757: E/AndroidRuntime(11652): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
04-22 11:37:21.757: E/AndroidRuntime(11652): ... 11 more
指導我這個請
檢查這個精心'父=「@風格/部件。 AppCompat.Light.ActionBar.Solid.Inverse「'沒有找到主題 –