ActionBarMenu更改颜色错误?

时间:2014-02-26 08:41:34

标签: java android android-actionbar

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

}
 }

我在这三行中有错误

getLayoutInflater().setFactory( new Factory() { 

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

new Factory()要求再次创建onCreateView,即使它是存在的 并且R.menu.main说它无法解决主要问题 最后一个错误Context和AttributeSet问我创建它们的类。

1 个答案:

答案 0 :(得分:0)

将品牌颜色添加到操作栏的最佳方法是创建自定义样式。一个很好的工具是Action Bar Style Generator