当前位置: 首页 > 知识库问答 >
问题:

造型动作栏使用主题。AppCompat公司

堵恺
2023-03-14

我正在接受android培训,设计动作栏。

由于android:theme=@style/MyActionBar,我面临错误。

我想知道如何设置动作栏的样式。

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

IllegalStateException:您需要使用主题。AppCompat主题与此活动

我认为错误来自:

<style name="CustomActionBarTheme"
               parent="@android:style/Theme.Holo.Light.DarkActionBar">
            <item name="android:actionBarStyle">@style/MyActionBar</item>
        </style>

所以我将其修改为

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

我有以下错误:

错误:检索项的父项时出错:未找到与给定名称“@android:style/Theme”匹配的资源。AppCompat。“灯光”。

根据培训说明,我创建了一个新文件

资源/价值观/主题。xml

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

    <!-- ActionBar styles -->

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

这是旅客名单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.helloworld"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
       android:theme="@style/MyActionBar" >
        <activity
            android:name="com.example.helloworld.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>
        <activity
        android:name="com.example.helloworld.DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.example.Helloworld.MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.Helloworld.MyActivity" />
    </activity>

    </application>

</manifest>

共有3个答案

莘翰采
2023-03-14

我认为您没有在项目中添加appcompat lib的引用。将它的引用添加到您的项目并检查

https://developer.android.com/tools/support-library/setup.html

容磊
2023-03-14

appcompat v7库有两种文件。一个是jar,另一个是res,类似字符串。xml样式。xml。您的问题意味着您没有导入appcompat库的res。因此,导入的项目/将Android SDK/SDK/extras/Android/support/v7/appcompat添加到Eclipse中,然后将其添加为主项目的库项目。

终弘厚
2023-03-14

编辑您的样式如下:

<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">@drawable/actionbar_background</item>

    <!-- Support library compatibility -->
    <item name="background">@drawable/actionbar_background</item>
</style>

对于必须使用的支持库

name="background">@drawable/actionbar_background</item> 

而不是

name="android:background">@drawable/actionbar_background</item>

然后在清单中使用此主题...使用前不要忘记添加支持:build.gradle如果您使用的是Android Studio...谢谢...

 类似资料:
  • 但当我运行:Caused by:java.lang.IllegalStateException时:您需要将一个theme.AppCompat主题(或后代)与此活动一起使用。 我不明白,谢谢^^

  • 我正在我的playground Android应用程序中实现一个黑暗主题,正在努力让动作栏文本颜色变成白色。

  • Android主题有什么不同。AppCompat和Base。主题AppCompat?我们什么时候应该使用基本主题?

  • 我正在创建一个应用程序,并发现了全息黑暗主题(我正在使用它为我的对话框)。看了之后,我想改变我的整个应用程序看起来像全息主题。问题是,在运行时,我得到一个错误,告诉我必须使用AppCompat主题,但我找不到AppCompat.Holo主题!有人知道如何强制AppCompat使用全息主题吗?

  • 我在启动应用程序时遇到以下错误: 我认为这与有关,但想不通。

  • 该项目的最小sdk为9,最大sdk为19。 我已在res/values文件夹中创建了一个xml文件: red_actionbar.xml 至 它确实改变了我应用程序的主题,所以问题出在风格上但我不知道如何解决。