public class MainActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = new MenuInflater(getApplicationContext()); //实例化MenuInflater
inflater.inflate(R.menu.menu_options, menu);
setMenuBackground();
return true;
}
// 关键代码就是重写Layout类的工厂方法onCreateView,这里对比绘制的View来替换系统中的
public void setMenuBackground(){
getLayoutInflater().setFactory( new Factory() {
@Override
public View onCreateView ( String name, Context context, AttributeSet attrs ) {
if ( name.equalsIgnoreCase( "com.android.internal.view.menu.IconMenuItemView" ) ) {
//上面这句不能有改动,目前来看对于原生android目前这个packageName还没有变化
try {
LayoutInflater f = getLayoutInflater();
final View view = f.createView( name, null, attrs ); //尝试创建我们自己布局
new Handler().post( new Runnable() {
public void run () {
view.setBackgroundResource(R.drawable.back); //设置背景为我们自定义的图片,替换cwj_bg文件即可
}
} );
return view;
}
catch ( InflateException e ) {}
catch ( ClassNotFoundException e ) {}
}
return null;
}
});
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onStop() {
super.onStop();
}
}
res/menu/options_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_game"
android:title="New Game" />
<item android:id="@+id/quit"
android:title="Quit" />
</menu>
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = new MenuInflater(getApplicationContext()); //实例化MenuInflater
inflater.inflate(R.menu.menu_options, menu);
setMenuBackground();
return true;
}
// 关键代码就是重写Layout类的工厂方法onCreateView,这里对比绘制的View来替换系统中的
public void setMenuBackground(){
getLayoutInflater().setFactory( new Factory() {
@Override
public View onCreateView ( String name, Context context, AttributeSet attrs ) {
if ( name.equalsIgnoreCase( "com.android.internal.view.menu.IconMenuItemView" ) ) {
//上面这句不能有改动,目前来看对于原生android目前这个packageName还没有变化
try {
LayoutInflater f = getLayoutInflater();
final View view = f.createView( name, null, attrs ); //尝试创建我们自己布局
new Handler().post( new Runnable() {
public void run () {
view.setBackgroundResource(R.drawable.back); //设置背景为我们自定义的图片,替换cwj_bg文件即可
}
} );
return view;
}
catch ( InflateException e ) {}
catch ( ClassNotFoundException e ) {}
}
return null;
}
});
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onStop() {
super.onStop();
}
}
res/menu/options_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_game"
android:title="New Game" />
<item android:id="@+id/quit"
android:title="Quit" />
</menu>