java - Android Actionbar 不应用自定义图像背景

标签 java android android-actionbar manifest

我正在使用 Android Studio,我尝试将自定义图像添加到操作栏背景中,现在我在 youtube 上使用了一些教程,其中说我应该将以下代码添加到我的 样式中。 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>

样式.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:

在 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>

现在在您的 list 中使用以下方法在您的 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();

替代方案

将图像添加到您的工具栏背景:

<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>

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

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

关于java - Android Actionbar 不应用自定义图像背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43696107/

相关文章:

java - 记录的 Matchers 多于预期 - Easymock 从 Maven 而不是从 Eclipse 失败

java - 阻止用户返回,但过滤器不起作用

ConstraintLayout 中的 Android Recyclerview 项目不显示

android - 如何更改 ActionBarSherlock 上 IcsSpinner 的文本颜色?

android - 操作栏在替换为 fragment 时向下移动

用于具有 @Deprecated 等语义的代码风格的 Java 注释

java - 如何将字符串日期转换为长毫秒

ANDROID - Activity 不会破坏主 Activity

android - 如何设置 ListView 分隔线的宽度?

android - 如何在 UI 设置时获取 ActionBar 菜单?