android - 更改底部菜单项背景

标签 android actionbarsherlock

单击设备菜单按钮后,如何更改出现在底部的菜单项的背景

enter image description here

我正在使用 ActionBarSherlock 并且顶部的 ActionBar 具有蓝色背景。认为底部菜单的背景也是如此。但它是白色的。看起来像默认。因此,该图标在那个白色背景上看起来不太好。我怎样才能把它变成蓝色。

我试过这个:

<item name="android:panelBackground">@drawable/blue</item>

但是没用。

谢谢。

最佳答案

我有类似的要求,我找到了这个可行的解决方案。使用以下内容

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    addOptionsMenuHackerInflaterFactory();
    return true;
}

@SuppressWarnings("rawtypes")
static Class       IconMenuItemView_class = null;
@SuppressWarnings("rawtypes")
static Constructor IconMenuItemView_constructor = null;

// standard signature of constructor expected by inflater of all View classes
@SuppressWarnings("rawtypes")
private static final Class[] standard_inflater_constructor_signature = 
new Class[] { Context.class, AttributeSet.class };

protected void addOptionsMenuHackerInflaterFactory()
{
    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; // use normal inflater

            View view = null;

            if (IconMenuItemView_class == null)
            {
                try
                {
                    IconMenuItemView_class = getClassLoader().loadClass(name);
                }
                catch (ClassNotFoundException e)
                {
                    // this OS does not have IconMenuItemView - fail gracefully
                    return null; // hack failed: use normal inflater
                }
            }
            if (IconMenuItemView_class == null)
                return null; // hack failed: use normal inflater

            if (IconMenuItemView_constructor == null)
            {
                try
                {
                    IconMenuItemView_constructor = 
                    IconMenuItemView_class.getConstructor(standard_inflater_constructor_signature);
                }
                catch (SecurityException e)
                {
                    return null; // hack failed: use normal inflater
                }
                catch (NoSuchMethodException e)
                {
                    return null; // hack failed: use normal inflater
                }
            }
            if (IconMenuItemView_constructor == null)
                return null; // hack failed: use normal inflater

            try
            {
                Object[] args = new Object[] { context, attrs };
                view = (View)(IconMenuItemView_constructor.newInstance(args));
            }
            catch (IllegalArgumentException e)
            {
                return null; // hack failed: use normal inflater
            }
            catch (InstantiationException e)
            {
                return null; // hack failed: use normal inflater
            }
            catch (IllegalAccessException e)
            {
                return null; // hack failed: use normal inflater
            }
            catch (InvocationTargetException e)
            {
                return null; // hack failed: use normal inflater
            }
            if (null == view) // in theory handled above, but be safe... 
                return null; // hack failed: use normal inflater


            // apply our own View settings after we get back to runloop
            // - android will overwrite almost any setting we make now
            final View v = view;
            new Handler().post(new Runnable()
            {
                public void run()
                {
                    v.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonselector));
                         // here use your own selector drawable 
                    try
                    {
                        // in Android <= 3.2, IconMenuItemView implemented with TextView
                        // guard against possible future change in implementation
                        TextView tv = (TextView)v;
                        tv.setTextColor(Color.WHITE);
                    }
                    catch (ClassCastException e)
                    {
                        // hack failed: do not set TextView attributes
                    }
                }
            });

            return view;
        }
    });
}

警告:

这是一个黑客解决方案。因为我需要它,一旦我不能给足够的时间来发现是否有任何副作用。但它对我很有帮助。希望对您有所帮助

关于android - 更改底部菜单项背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15402150/

相关文章:

android - 如何自定义共享菜单

android - 如何为整个应用程序自定义主题

list 中的android anydensity

android - 在 Android Google Map 中将 map 标记置于最前面

android - 将 RecyclerView 与卡片一起使用

android - 无法添加依赖 Android Studio

Android 启动画面 - 操作栏出现一会儿然后消失

android - 尝试使用 NSight 编译 Android 非 native 项目

AndroidPlot:无法在 Android 应用程序中显示列表数据的折线图

android - 清理 ActionBarSherlock 创建 R cannot be resolved 错误