【问题标题】:Android Custom ActionBar StyleAndroid 自定义 ActionBar 样式
【发布时间】:2015-01-27 12:40:54
【问题描述】:

我正在尝试为我的 android 应用程序制作自定义主题。但问题是我无法更改操作栏主题。应用样式时,它给了我以下错误并关闭了应用程序:

 java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

我不明白我做错了什么。这是我的代码:

AndroidManifest.xml

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/AcelinkTheme">
        <activity
            android:name=".Dashboard"
            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>

res/values/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="android:Theme.Holo.Light">
        <!-- Customize your theme here. -->
    </style>

</resources>

res/values/themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="AcelinkTheme"
        parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/AcelinkActionBar</item>
    </style>

    <!-- colors -->
    <color name="aceblue">#438eb9</color>

    <!-- ActionBar styles -->
    <style name="AcelinkActionBar"
        parent="android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">@color/aceblue</item>
    </style>
</resources>

【问题讨论】:

  • 您的 logcat 明确表示,如果您使用 appcompact -v7 library,则需要为您的应用使用 Appcompact 主题

标签: android xml themes


【解决方案1】:

例外很明显:您需要使用继承自Theme.AppCompat 的主题,可能是因为您使用的是ActionBarActivity,这需要the docs 中指定的主题。

你可以这样做:

<style name="AcelinkTheme" parent="@android:style/Theme.AppCompat.Light">
    <item name="android:actionBarStyle">@style/AcelinkActionBar</item>
</style>

<!-- ActionBar styles -->
<style name="AcelinkActionBar" parent="@android:style/Widget.AppCompat.ActionBar">
    <item name="android:background">@color/aceblue</item>
</style>

为清楚起见,最好在自己的资源文件恕我直言中定义颜色。

【讨论】:

    【解决方案2】:

    根据这个thread,您应该将您的父级更改为 Theme.AppCompat 主题派生:

    <style name="AcelinkTheme"
        parent="@style/Theme.AppCompat">
        <item name="android:actionBarStyle">@style/AcelinkActionBar</item>
    </style>
    

    此链接还可以帮助您自定义主题:Styling the Action Bar :)

    【讨论】:

    猜你喜欢
    • 2014-01-25
    • 1970-01-01
    • 2013-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多