【问题标题】:How to set background in option menu in android all versions如何在android所有版本的选项菜单中设置背景
【发布时间】:2015-07-13 11:48:23
【问题描述】:

我正在尝试使用图标实现选项菜单,但我试图在选项菜单上设置背景但没有成功如何实现请帮助我

【问题讨论】:

标签: android android-layout android-activity layout


【解决方案1】:
  @Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater inflater=getMenuInflater();
    inflater.inflate(R.menu.menu,menu);
   setMenuBackground(); 
   return true;    
 }




  protected void setMenuBackground(){                     
    // Log.d(TAG, "Enterting setMenuBackGround");  
    getLayoutInflater().setFactory( new Factory() {  
        public View onCreateView(String name, Context context, AttributeSet attrs) {
            if ( name.equalsIgnoreCase( "com.android.internal.view.menu.IconMenuItemView" ) ) {
                try { // Ask our inflater to create the view  
                    LayoutInflater f = getLayoutInflater();  
                    final View view = f.createView( name, null, attrs );  
                    /* The background gets refreshed each time a new item is added the options menu.  
                    * So each time Android applies the default background we need to set our own  
                    * background. This is done using a thread giving the background change as runnable 
                    * object */
                    new Handler().post( new Runnable() {  
                        public void run () {  
                            // sets the background color   
                            view.setBackgroundResource( R.color.androidcolor);
                            // sets the text color              
                            ((TextView) view).setTextColor(Color.BLACK);
                            // sets the text size              
                            ((TextView) view).setTextSize(18);
            }
                    } );  
                return view;
            }
        catch ( InflateException e ) {}
        catch ( ClassNotFoundException e ) {}  
    } 
    return null;
}}); 
}

【讨论】:

    【解决方案2】:

    ActionBarStyleGenerator

    您需要在哪里设置弹出颜色,它将根据需要更改菜单背景。下载生成的样式并应用到您的项目中。

    【讨论】:

    • 我正在使用这个,但我的操作栏搜索视图不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-10
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    相关资源
    最近更新 更多