我们如何用布局动态地在android中充气特定的菜单项

时间:2012-02-06 08:42:07

标签: android android-layout

我使用下面的代码来设置背景,但它将它应用于整个菜单而不是一个菜单项。我们是否可以修改以下代码以仅对一个菜单项进行充气...

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

0 个答案:

没有答案