2014-02-26 52 views
0
package com.cartperk.android.cartperk.ui; 

    import android.R; 
    import android.annotation.SuppressLint; 
    import android.app.Activity; 
    import android.content.res.Resources; 
    import android.graphics.Color; 
    import android.graphics.drawable.ColorDrawable; 
    import android.os.Bundle; 
    import android.os.Handler; 
    import android.view.InflateException; 
    import android.view.LayoutInflater; 
    import android.view.LayoutInflater.Factory; 
    import android.view.Menu; 
    import android.view.View; 
    import android.widget.TextView; 

    public class MenuActionBar extends Activity { 

@SuppressLint("NewApi") 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    getActionBar().setBackgroundDrawable(new 
       ColorDrawable(Color.parseColor("#33B5E5"))); 

    int titleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android"); 
    TextView titleText = (TextView)findViewById(titleId); 
    titleText.setTextColor(Color.parseColor("#ffffff")); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // TODO Auto-generated method stub 
    getMenuInflater().inflate(R.menu.main, menu); 
     setMenuBackground(); 
     return true; 
} 

private void setMenuBackground() { 
    // TODO Auto-generated method stub 
    getLayoutInflater().setFactory(new Factory() { 

     @Override 
     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.menubg); 
          // sets the text color    
          ((TextView) view).setTextColor(Color.WHITE); 
          // sets the text size    
          ((TextView) view).setTextSize(18); 
      } 
        }); 
       return view; 
      } 
     catch (InflateException e) {} 
     catch (ClassNotFoundException e) {} 
    } 
      return null; 
     }}); 

} 
} 

我在這三行ActionBarMenu更改顏色錯誤?

getLayoutInflater().setFactory(new Factory() { 

     @Override 
     public View onCreateView(String name, Context context,AttributeSet attrs) 

new Factory()錯誤被要求再次創建onCreateView甚至認爲它是目前 和R.menu.main說,它不能解析到主 和最後的錯誤語境的AttributeSet是問我創建他們的類。

回答