不同的外观/风格的动作条上特定的菜单项 [英] Different look/style for specific menu item on ActionBar

查看:96
本文介绍了不同的外观/风格的动作条上特定的菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用程序,我自定义动作条以下的的 Android开发者博客等地。我想是能够突出在动作条比如一个特定的菜单项用不同的背景颜色,因为它特别适用于一个当前可见片段(其实际添加的话)。但是我还没有发现任何迹象表明,这是可以做到我也没有成功地做到这一点我自己呢。可能吗?如果是的话怎么样?

On my Android application I am customizing the ActionBar following tips from the android developer blog and other places. I would like to be able to highlight a specific menu item on the ActionBar e.g. with a different background color because it is especially applicable to a currently visible fragment (which actually added it). However I have not found any indication that this can be done nor have I managed to do so myself yet. Is it possible? If so how?

推荐答案

所以我就这一个有点一招,它现在的工作。这里所说的片段code

So I got this to work now with a bit of a trick to it. Here goes the fragment code

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);

    menuItemCreateCart = menu.findItem(R.id.menuItemCreateCart);
    if (menuItemCreateCart  == null) {
        menuItemCreateCart  = menu.add(0, R.id.menuItemCreateCart, 0, R.string.Create);
    }

    TextView tv = new TextView(getActivity());
    tv.setText(R.string.Create);
    tv.setTextColor(getResources().getColor(R.color.green));
    tv.setBackgroundColor(getResources().getColor(R.color.lightBlue));
    tv.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    createCart();
                }
            }
    );
    menuItemCreateCart.setActionView(tv);

主要的疑难杂症是,onclicklistener,必须对您设置为动作视图,而不是菜单项,它的工作视图设置。这样,你可以做任何你喜欢的。

The main gotcha is that the onclicklistener has to be set on the view that you set as action view and not the menu item for it to work. This way you can do whatever you like.

另外请注意,您不能使用getActionView检索与标题最初设置视图,因为它会返回null。它似乎更比默认菜单项目的实际来看另一种观点的。

Also note that you can NOT use getActionView to retrieve the originally set view with title because it will return null. It seem to be more of an alternative view than the actual view for default menu items..

这篇关于不同的外观/风格的动作条上特定的菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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