如何更改选项菜单样式

时间:2013-06-03 14:31:44

标签: android

我已经完成了我的选项菜单,但它显示为列表视图,但我需要像Button矩形这样的样式。

我的选项菜单 enter image description here

我需要选项菜单这样 enter image description here

如何更改选项菜单样式?

此致

2 个答案:

答案 0 :(得分:0)

这就是我的做法:

public boolean onCreateOptionsMenu(Menu menu)
        {
            CambiarFondoMenuInflater();
           return Menus.CrearMenus(contexto,menu, TipoMenu.PRINCIPAL);
        }

CambiarFondoMenuInflater()方法:

protected void CambiarFondoMenuInflater()
    {
        final LayoutInflater infl = getLayoutInflater();

        infl.setFactory(new Factory()
        {
            public View onCreateView(final String name, 
                                     final Context context,
                                     final AttributeSet attrs)
            {
                if (!name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView"))
                    return null; 
                View view = null;

                if (IconMenuItemView_class == null)
                {
                    try
                    {
                        IconMenuItemView_class = getClassLoader().loadClass(name);
                    }
                    catch (ClassNotFoundException e)
                    {

                        return null;
                    }
                }
                if (IconMenuItemView_class == null)
                    return null;

                if (IconMenuItemView_constructor == null)
                {
                    try
                    {
                        IconMenuItemView_constructor = 
                        IconMenuItemView_class.getConstructor(standard_inflater_constructor_signature);
                    }
                    catch (SecurityException e)
                    {
                        return null;
                    }
                    catch (NoSuchMethodException e)
                    {
                        return null;
                    }
                }
                if (IconMenuItemView_constructor == null)
                    return null; 

                try
                {
                    Object[] args = new Object[] { context, attrs };
                    view = (View)(IconMenuItemView_constructor.newInstance(args));
                }
                catch (IllegalArgumentException e)
                {
                    return null; 
                }
                catch (InstantiationException e)
                {
                    return null; 
                }
                catch (IllegalAccessException e)
                {
                    return null; 
                }
                catch (InvocationTargetException e)
                {
                    return null; 
                }
                if (null == view) 
                    return null; 


                final View v = view;
                new Handler().post(new Runnable()
                {
                    public void run()
                    {
                        v.setBackgroundColor(Color.BLACK);

                        try
                        {
                           // Here you can change whatever you want :)
                            TextView tv = (TextView)v;
                            tv.setTextColor(Color.WHITE);
                        }
                        catch (ClassCastException e)
                        {
                            // Don't set TextView attributes
                        }
                    }
                });

                return view;
            }
        });
    }

此信息取自本网站的帖子。我已经搜索过但我找不到它,所以我在这里放了我的代码。如果有人可以参考它,我会非常感激。

答案 1 :(得分:0)

只需删除文件夹值-v11,values-v14等, 不要删除默认值文件夹。