将菜单背景设置为不透明

时间:2011-04-11 12:54:13

标签: android

我正在尝试将菜单窗格的背景设置为不透明。经过大量的搜索和尝试,我几乎可以通过设置'panelFullBackground'来实现它,但是,我得到了丢失菜单窗格的上边缘及其阴影效果的不必要结果。我猜测只要设置颜色我就会失去与'panelFullBackground'相关的其他风格。

我已将应用程序设置为自定义样式。该样式继承自Theme.Light。 在我的风格中,我将'panelFullBackground'设置为自定义颜色,如下所示:

在stylex.xml中:

<item name="android:panelFullBackground">@color/custom_theme_color</item>

在colors.xml中

   <color name="custom_theme_color">#ff00ffff</color>

我尝试过使用panelColorForeground | Background但它们没有实现我想要的效果。

应该提到的是,我尝试使用其他地方建议的setMenuBackground方法无济于事。

提前致谢。

4 个答案:

答案 0 :(得分:0)

您可以更改Android菜单选项的背景。它只会以默认方式显示。如果要更改它们的外观,请考虑为菜单构建自定义视图。

答案 1 :(得分:0)

尝试在Codeproject找到的这个简单教程Android-Menus-My-Way,可能对您有用

答案 2 :(得分:0)

根据Louis Semprini的建议here::

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

        // "com.android.internal.view.menu.IconMenuItemView" 
        // - is the name of an internal Java class 
        //   - that exists in Android <= 3.2 and possibly beyond
        //   - that may or may not exist in other Android revs
        // - is the class whose instance we want to modify to set background etc.
        // - is the class we want to instantiate with the standard constructor:
        //     IconMenuItemView(context, attrs)
        // - this is what the LayoutInflater does if we return null
        // - unfortunately we cannot just call:
        //     infl.createView(name, null, attrs);
        //   here because on Android 3.2 (and possibly later):
        //   1. createView() can only be called inside inflate(),
        //      because inflate() sets the context parameter ultimately
        //      passed to the IconMenuItemView constructor's first arg,
        //      storing it in a LayoutInflater instance variable.
        //   2. we are inside inflate(),
        //   3. BUT from a different instance of LayoutInflater (not infl)
        //   4. there is no way to get access to the actual instance being used
        // - so we must do what createView() would have done for us
        //
        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.setBackgroundColor(Color.BLACK);

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

要使用此代码,请从您的活动onCreate()或您的活动onCreateOptionsMenu()调用addOptionsMenuHackerInflaterFactory()ONCE。

守则运作完美!希望这会有所帮助!!

答案 3 :(得分:0)

您可以从@color@drawable的价值,如下所示对我有用:

<style name="MyTheme" parent="@android:style/Theme.Holo.Light">

    <!-- Menu Style -->
    <item name="android:panelBackground">@drawable/blue_background</item>

</style>