Android 操作栏不应用自定义图像背景

Android Actionbar doesn't apply custom image background

我正在使用 Android Studio,我尝试将自定义图像添加到操作栏背景中,现在我在 youtube 上使用了一些教程,其中说我应该将以下代码添加到我的 styles.xml

 <style name="custom_actionbar" parent="@android:style/Theme.Holo.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/custom_style</item>
    </style>
    <style name="custom_style" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@drawable/bar</item>
    </style>

和manifest.xml

<activity
       android:name=".MainActivity"
       android:label="@string/app_name"
       android:screenOrientation="portrait"
       android:theme="@style/custom_actionbar">

现在,虽然它向我显示此错误,但它应该可以正常工作:

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

所以我在互联网上查找并找到了 this,但在使用解决方案后,应用程序仍然崩溃并出现相同的错误(我使用调试来检查使用解决方案后几次出错)。

如何才能正确使用 Theme.AppCompact 主题以便应用程序正常运行?

我的整个manifest.xml

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Food"
            android:label="@string/title_activity_food"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".Time"
            android:label="@string/title_activity_time"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".Info"
            android:screenOrientation="portrait" />
        <activity
            android:name=".Training"
            android:label="@string/title_activity_training"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity android:name=".firstEntry"></activity>
    </application>

Styles.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="android:actionBarStyle">@style/custom_style</item>
    </style>

    <style name="custom_style" parent="@style/Widget.AppCompat.Light.ActionBar">
        <item name="android:background">@drawable/bar</item>
    </style>

    <style name="LightDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="android:textColor">@android: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>

尝试使用 AppCompatTheme:

in res/values/styles.xml : 将你的样式替换为

 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="custom_actionbar" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/custom_style</item>
</style>

<style name="custom_style" parent="@style/Widget.AppCompat.Light.ActionBar">
    <item name="android:background">@drawable/bar</item>
</style>

现在在您的清单中添加自定义操作栏主题 activity 使用:

 <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/custom_actionbar">

编辑

在你的主要activity中修改:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    //setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    prefs = getSharedPreferences("com.none.myapplication", MODE_PRIVATE);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

备选方案

将图像添加到您的 toolbar 背景:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@drawable/bar"  //your image
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.v7.widget.Toolbar>

同时修改清单中的主题:使用 toolbar 而不是 ActionBar

<activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar">