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

3
在我的主页活动中,我有一个带有自定义溢出菜单的工具栏,我想让它的颜色与工具栏背景颜色相符(因为工具栏背景颜色可以更改(用户更改为自己喜欢的颜色))...我不知道该怎么做:
这是我的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

1个回答

1

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

@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; 
}

如果背景是深色的,文本颜色就是白色;如果背景是浅色的,文本颜色就是黑色。下面是两个工具栏颜色不同的屏幕:红色工具栏 绿色工具栏 希望对其他人有所帮助,Darkball60(如果你喜欢这个答案,请随意点赞:))。

没问题。实际上,直接在Java中动态更改它是相当困难的,需要仔细思考才能解决。 - Mesabloo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接