自定义主题未生效(Android)

时间:2015-03-20 08:07:25

标签: android android-actionbar

在我的styles.xml中 - 在values文件夹下:

<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="android:actionOverflowButtonStyle">@style/MyActionBar</item>
    <item name="android:windowBackground">@android:color/white</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>

<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">true</item>
    <item name="color">@android:color/white</item>
</style>



<!-- Overflow menu -->
<!-- ActionBar styles -->
<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">#2E6154</item>
    <item name="android:windowActionBarOverlay">true</item>
    <item name="android:icon">@android:color/transparent</item>
    <item name="android:src">@drawable/ic_overflow</item>
    <item name="android:actionButtonStyle">@style/MyActionButtonStyle</item>
    <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
</style>

<style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">#FFFFFF</item>
</style>

<style name="MyActionButtonStyle" parent="@android:style/Widget.ActionButton">
    <item name="android:minWidth">0dip</item>
    <item name="android:paddingLeft">0dip</item>
    <item name="android:paddingRight">0dip</item>
</style>
清单文件中的

 <uses-sdk
            android:minSdkVersion="14"
            android:targetSdkVersion="18" />

 <application
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/CustomActionBarTheme" >

我还怀疑罪魁祸首是parent="@style/Theme.AppCompat"但无法找到参考。

然而,当我在5.0.1上运行应用程序时,风格没有任何影响。 我想我可能不得不在values-14文件夹中更改它,但在此之前我尝试在4.0.1设备上运行应用程序,没有任何效果。我在这里想念什么?

之前我正在使用Holo Light主题并且一切正常,只要我将主题更改为AppCompact,定制似乎就丢失了。我在这里想念什么?

修改

如果我将主题更改为: <style name="CustomActionBarTheme" parent="@android:style/Theme.Holo.Light">

我得到以下内容:

03-20 15:01:34.433: E/AndroidRuntime(9647): Process: com.exa, PID: 9647
03-20 15:01:34.433: E/AndroidRuntime(9647): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.exa/com.exa.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
03-20 15:01:34.433: E/AndroidRuntime(9647):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
03-20 15:01:34.433: E/AndroidRuntime(9647):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
03-20 15:01:34.433: E/AndroidRuntime(9647):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
03-20 15:01:34.433: E/AndroidRuntime(9647):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
03-20 15:01:34.433: E/AndroidRuntime(9647):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-20 15:01:34.433: E/AndroidRuntime(9647):     at android.os.Looper.loop(Looper.java:135)
03-20 15:01:34.433: E/AndroidRuntime(9647):     at android.app.ActivityThread.main(ActivityThread.java:5221)
03-20 15:01:34.433: E/AndroidRuntime(9647):     at java.lang.reflect.Method.invoke(Native Method)
03-20 15:01:34.433: E/AndroidRuntime(9647):     at java.lang.reflect.Method.invoke(Method.java:372)
03-20 15:01:34.433: E/AndroidRuntime(9647):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
03-20 15:01:34.433: E/AndroidRuntime(9647):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-20 15:01:34.433: E/AndroidRuntime(9647): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
03-20 15:01:34.433: E/AndroidRuntime(9647):     at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:152)
03-20 15:01:34.433: E/AndroidRuntime(9647):     at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:149)
03-20 15:01:34.433: E/AndroidRuntime(9647):     at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
03-20 15:01:34.433: E/AndroidRuntime(9647):     at com.exa.MainActivity.onCreate(MainActivity.java:428)
03-20 15:01:34.433: E/AndroidRuntime(9647):     at android.app.Activity.performCreate(Activity.java:5933)
03-20 15:01:34.433: E/AndroidRuntime(9647):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
03-20 15:01:34.433: E/AndroidRuntime(9647):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
03-20 15:01:34.433: E/AndroidRuntime(9647):     ... 10 more

1 个答案:

答案 0 :(得分:0)

你在风格上有很多错误,要使用你必须做的appcompat主题:

<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat">
    <item name="actionBarStyle">@style/MyActionBar</item>
    <item name="actionOverflowButtonStyle">@style/MyActionBar</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>

<style name="DrawerArrowStyle" parent="@style/Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">true</item>
    <item name="color">..</item>
</style>

<!-- Overflow menu -->
<!-- ActionBar styles -->
<style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">
    <item name="background">..</item>
    <item name="windowActionBarOverlay">true</item>
    <item name="icon">..</item>
    <item name="actionButtonStyle">@style/MyActionButtonStyle</item>
    <item name="titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
</style>

<style name="MyTheme.ActionBar.TitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">..</item>
</style>

<style name="MyActionButtonStyle" parent="@style/Widget.AppCompat.ActionButton">
    <item name="android:minWidth">0dip</item>
    <item name="android:paddingLeft">0dip</item>
    <item name="android:paddingRight">0dip</item>
</style>