【问题标题】:Setting the menu background to be opaque将菜单背景设置为不透明
【发布时间】:2023-03-06 15:37:01
【问题描述】:

我正在尝试将菜单窗格的背景设置为不透明。经过大量搜索和尝试后,我几乎可以通过设置“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 方法无济于事。

提前致谢。

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以更改 android 菜单选项的背景。它只会以默认方式查看。如果您想更改它们的外观和感觉,请考虑为菜单构建自定义视图。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        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() 一次。

        代码完美运行!希望这会有所帮助!

        【讨论】:

          【解决方案4】:

          您可以从@color@drawable 的值,如下所示:

          <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
          
              <!-- Menu Style -->
              <item name="android:panelBackground">@drawable/blue_background</item>
          
          </style>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-08-31
            • 2018-07-03
            • 2021-03-04
            • 1970-01-01
            • 2018-08-04
            • 1970-01-01
            • 2013-01-11
            • 2014-04-05
            相关资源
            最近更新 更多