使用Java代码更改自定义SubMenu背景颜色 [英] Change custom SubMenu background color with Java code

查看:99
本文介绍了使用Java代码更改自定义SubMenu背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my home activity, I have a toolbar with a custom Overflow menu and I want it to change color to be accorded with toolbar background color (cause toolbar background color can change (user changes to his own favorite color))...I don't know how to do it :

这是我的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);
}

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

I've already tried to change background color of each element...but I got NPE each time, with menu.findItem(R.id.overflow_about_us).getActionView().setBackgroundColor(Color.BLUE);

希望您会找到适合我的解决方案Darkball60

Hope you'll find a solution for me, Darkball60

推荐答案

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

So...no one helped me...so I had to work alone...hours and hours...to finally find the solution :

@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为黑色

If background is dark...so textColor is white...and if background is light, textColor is black.

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

Below are two screens with different toolbar color :

希望它可以帮助我以外的其他人,Darkball60(如果您喜欢答案...可以随意投票:))

Hope it helps someone else than me, Darkball60 (and if you like the answer...feel free to upvote :))

这篇关于使用Java代码更改自定义SubMenu背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆