java Android Actionbar不应用自定义图像背景
我使用的是Android Studio,我尝试将自定义图像添加到操作栏背景中,现在我在youtube上使用了一些教程,其中说我应该将以下代码添加到我的styles.xml
<style name="custom_actionbar" parent="@安卓:style/Theme.Holo.Light.DarkActionBar">
<item name="安卓:actionBarStyle">@style/custom_style</item>
</style>
<style name="custom_style" parent="@安卓:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="安卓:background">@drawable/bar</item>
</style>
并显示。xml
<activity
安卓:name=".MainActivity"
安卓:label="@string/app_name"
安卓:screenOrientation="portrait"
安卓:theme="@style/custom_actionbar">
现在,它应该可以工作了,尽管它显示了以下错误:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity
所以我在互联网上查了一下,发现了this,尽管在使用了解决方案之后,应用程序仍然会因为同样的错误而崩溃(在使用解决方案之后,我已经用调试检查了几次错误)
如何使用主题。AppCompact主题是否正确,应用程序能否正常工作
我的全部舱单。xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
package="com.none.myapplication">
<application
安卓:allowBackup="true"
安卓:icon="@mipmap/ic_launcher"
安卓:label="@string/app_name"
安卓:supportsRtl="true"
安卓:theme="@style/AppTheme">
<activity
安卓:name=".MainActivity"
安卓:label="@string/app_name"
安卓:screenOrientation="portrait"
安卓:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action 安卓:name="安卓.intent.action.MAIN" />
<category 安卓:name="安卓.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
安卓:name=".Food"
安卓:label="@string/title_activity_food"
安卓:screenOrientation="portrait"
安卓:theme="@style/AppTheme.NoActionBar" />
<activity
安卓:name=".Time"
安卓:label="@string/title_activity_time"
安卓:screenOrientation="portrait"
安卓:theme="@style/AppTheme.NoActionBar" />
<activity
安卓:name=".Info"
安卓:screenOrientation="portrait" />
<activity
安卓:name=".Training"
安卓:label="@string/title_activity_training"
安卓:theme="@style/AppTheme.NoActionBar" />
<activity 安卓:name=".firstEntry"></activity>
</application>
风格。xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>
<style name="custom_actionbar" parent="@style/Theme.AppCompat.Light">
<item name="安卓:actionBarStyle">@style/custom_style</item>
</style>
<style name="custom_style" parent="@style/Widget.AppCompat.Light.ActionBar">
<item name="安卓:background">@drawable/bar</item>
</style>
<style name="LightDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="安卓:textColor">@安卓:color/primary_text_light</item>
<item name="background">@drawable/bar</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
# 1 楼答案
尝试使用
AppCompatTheme
:在res/值/样式中。xml:将您的样式替换为
现在,在清单中,使用以下命令在活动中添加自定义操作栏主题:
编辑
在主活动中,修改:
替代解决方案
将图像添加到
toolbar
背景中:还要修改清单中的主题:使用
toolbar
而不是ActionBar