【问题标题】:Change bottom menu item background更改底部菜单项背景
【发布时间】:2013-03-14 15:16:38
【问题描述】:

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

我正在使用 ActionBarSherlock,顶部的 ActionBar 有蓝色背景。以为底部菜单的背景也是如此。但它是白色的。看起来像默认值。所以,这个图标在那个白色背景上看起来不太好。如何将其更改为蓝色。

我试过了:

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

但是没有用。

谢谢。

【问题讨论】:

  • 您想更改应用菜单项的背景>?还是整个系统?只有改变你的应用程序就可以了?
  • 是的,只是为了我的应用程序。 ABS Theme Generator:这里的顶部和底部操作栏使用相同的颜色。底部操作栏是否与我通过单击设备菜单按钮得到的菜单项有什么不同?

标签: android actionbarsherlock


【解决方案1】:

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

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

注意:

这是一个黑客解决方案。因为我需要它,一旦我没有足够的时间来确定是否有任何副作用。但它完美地为我服务。希望对你有帮助

【讨论】:

  • 什么是 IconMenuItemView_constructor? standard_inflater_constructor_signature 来自哪里?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-25
相关资源
最近更新 更多