java - 使用 Java 代码更改自定义子菜单背景颜色

标签 java android xml nullpointerexception android-menu

在我的主页 Activity 中,我有一个带有自定义溢出菜单 的工具栏,我希望它更改颜色以与工具栏背景颜色一致(因为工具栏背景颜色可以更改(用户更改为他自己喜欢的颜色))...我不知道该怎么做:

这是我的overflow_menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/overflow_save_current" app:showAsAction="always" android:title="@string/overflow_save_current" android:icon="@drawable/ic_action_save_light" />
<item android:id="@+id/overflow_settings" app:showAsAction="always" android:title="@string/overflow_settings" android:icon="@drawable/ic_action_settings_light" />
<item android:id="@+id/overflow_overflow" app:showAsAction="always" android:icon="@drawable/ic_action_overflow_light">
    <menu>
        <item android:id="@+id/overflow_feed_back" app:showAsAction="never|withText" android:title="@string/overflow_feed_back" android:icon="@drawable/ic_action_send_now_light" />
        <item android:id="@+id/overflow_about_us" app:showAsAction="never|withText" android:title="@string/overflow_about_us" android:icon="@drawable/ic_action_about_light" />
        <item android:id="@+id/overflow_exit" app:showAsAction="never|withText" android:title="@string/overflow_exit" android:icon="@drawable/ic_action_forward_light" />
    </menu>
</item>

这里是 OnPrepareOptionsMenu() 方法:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    /* Resources res = getResources();
    item = menu.findItem(R.id.overflow_feed_back);
    SpannableStringBuilder builder = new SpannableStringBuilder("* " + res.getString(R.string.overflow_feed_back));
    builder.setSpan(new ImageSpan(this, android.R.drawable.ic_menu_send), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    item.setTitle(builder); */
    if (!Common.compatible(Common.color, 0xFF000000)) {
        menu.findItem(R.id.overflow_save_current).setIcon(R.drawable.ic_action_save_dark);
        menu.findItem(R.id.overflow_settings).setIcon(R.drawable.ic_action_settings_dark);
        menu.findItem(R.id.overflow_overflow).setIcon(R.drawable.ic_action_overflow_dark);
    } else {
        menu.findItem(R.id.overflow_save_current).setIcon(R.drawable.ic_action_save_light);
        menu.findItem(R.id.overflow_settings).setIcon(R.drawable.ic_action_settings_light);
        menu.findItem(R.id.overflow_overflow).setIcon(R.drawable.ic_action_overflow_light);
    }
    return super.onPrepareOptionsMenu(menu);
}

我已经尝试更改每个元素的背景颜色...但我每次都遇到 NPE,使用 menu.findItem(R.id.overflow_about_us).getActionView().setBackgroundColor(Color.BLUE) ;

希望你能为我找到解决方案,Darkball60

最佳答案

所以...没有人帮助我...所以我不得不独自工作...几个小时...终于找到解决方案:

@Override
public View onCreateView(String name, Context context, AttributeSet attrs)  {
    // Do you own inflater stuff here 
    // Check for menu items (Options menu AKA menu key) 
    if (name.equalsIgnoreCase("android.support.v7.view.menu.ListMenuItemView")) {
        try { 
            // Ask our inflater to create the view 
            final View view = LayoutInflater.from(context).createView(name, null, attrs); 
            // Kind of apply our own background 
            new Handler().post(new Runnable() {
                public void run() {
                    if (!Common.compatible(Common.color, 0xFF000000)) {
                        try {
                            ((TextView)((RelativeLayout)((ListMenuItemView)view).getChildAt(1)).getChildAt(0)).setTextColor(0xFFFFFFFF);
                        } catch (ClassCastException e) {

                        }
                    } else {
                        try {
                            ((TextView)((RelativeLayout)((ListMenuItemView)view).getChildAt(1)).getChildAt(0)).setTextColor(0xFF000000);
                        } catch (ClassCastException e) {

                        }
                    }
                    view.setBackgroundColor(Common.color);
                }
            });
            return view;
        } catch (InflateException e) {

        } catch (ClassNotFoundException e) {

        }
    }
    return null; 
}

如果背景是深色的……那么 textColor 就是白色的……如果背景是浅色的,那么 textColor 就是黑色的。

下面是两个工具栏颜色不同的屏幕:Red toolbar Green toolbar

希望它对我以外的其他人有所帮助,Darkball60(如果你喜欢这个答案......请随意投票 :))

关于java - 使用 Java 代码更改自定义子菜单背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39041524/

相关文章:

java - Spring 启动: How to avoid too many JPA repositories for each domain class

java - 如何在java类中获得相同的@XmlElement名称

java - Android操作栏自定义颜色样式

java - 如何在 JFrame 中捕获 JDialogs 窗口关闭事件

java.lang.NoClassDefFoundError : com. 谷歌.common.base.CharMatcher

java - View.getWidth() 显示正确的宽度,但在添加到 View 的 x 位置时无法产生右边距

java - 膨胀 Facebook 登录按钮时出错。

Android - 键盘显示/隐藏时不调用 onConfigurationChanged

java - 如何修复 Oreo 的 AOSP 构建错误?

java - 天堂操作系统 : Tasker Java Function "setActiveProfile(java.lang.String)"