更改动作条菜单图标取决于风格图标、菜单、风格、动作

2023-09-12 01:54:34 作者:ㄣ不好ci╰☆ぷ

我想用这取决于哪种风格我用(或深或浅)不同的动作条图标。 我无法弄清楚如何,继承人是我的尝试:

I want to use different ActionBar Icons depending on which style I use (Dark or Light). I couldn't figure it out how, heres what I tried:

<style name="AppThemeDark" parent="Theme.Sherlock.ForceOverflow">
        <item name="android:actionButtonStyle">@style/customActionButtonStyleDark</item>
        <item name="actionButtonStyle">@style/customActionButtonStyleDark</item>
    </style>

    <style name="AppThemeLight" parent="Theme.Sherlock.Light.ForceOverflow">
        <item name="android:actionButtonStyle">@style/customActionButtonStyleLight</item>
        <item name="actionButtonStyle">@style/customActionButtonStyleLight</item>
    </style>

    <style name="customActionButtonStyleDark" >
        <item name="@drawable/action_search">@drawable/action_search</item>
    </style>

    <style name="customActionButtonStyleLight" >
        <item name="@drawable/action_search">@drawable/action_search_light</item>
    </style>

我也试图插入&LT;项目名称=@可绘制/ ACTION_SEARCH&GT; @可绘制/ ACTION_SEARCH&LT; /项目&GT; 直接进入主题风格,但没有工作。 任何想法如何?

I also tried to insert <item name="@drawable/action_search">@drawable/action_search</item> directly into the theme style, but nothing worked. Any ideas how?

推荐答案

解决了这个问题,但放弃了与XML的尝试,我做到了现在的编程方式:

Solved it, but gave it up to try it with XML, I did it now programmatically:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        SharedPreferences prefs = getSharedPreferences("app", 0);
        boolean isDark = "Dark".equals(prefs.getString("theme", "Dark"));

        com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.main, menu);

        // set Icons
        menu.findItem(R.id.menuitem_search).setIcon(isDark ? R.drawable.action_search : R.drawable.action_search_light);
        menu.findItem(R.id.menuitem_add).setIcon(isDark ? R.drawable.content_new : R.drawable.content_new_light);
        menu.findItem(R.id.menuitem_share).setIcon(isDark ? R.drawable.social_share : R.drawable.social_share_light);
        return true;
    }