2011-04-11 118 views
3

我正在尝试设置菜单窗格的背景是不透明的。经过大量的搜索和尝试后,我几乎可以通过设置'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 |背景,但他们没有达到我想要的。

应该提到的是,我试图用setMenuBackground办法在其他地方都无济于事建议。

在此先感谢。

回答

0

您可以更改Android的菜单选项的背景。它只会以默认方式显示。如果你想改变它们的外观和感觉,可以考虑为菜单建立一个自定义视图。

0

有用的建议由路易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调用一次addOptionsMenuHackerInflaterFactory() ()或您的活动onCreateOptionsMenu()。

代码工作完美!希望这可以帮助 !!

0

您可以从@color重视到@drawable像下面这些工作对我来说:

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

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

</style>