我在 android 菜单中添加了设置选项。它在模拟器中测试时工作正常。但是当我尝试在设备中它不会改变..我能知道是什么问题吗?我是否需要更改 androidmanifest.xml 文件中的任何内容。
以下是我的代码:
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(1,1,0,"Settings").setIcon(R.drawable.ic_tab_settings_grey);
setMenuBackground();
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case 1:
Intent in = new Intent(TransactionSummaryActivity.this, WelcomePage.class);
startActivity(in);
finish();
return true;
default:
return true;
}
}
protected void setMenuBackground(){
getLayoutInflater().setFactory( new Factory() {
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
// TODO Auto-generated method stub
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 );
new Handler().post( new Runnable() {
public void run () {
view.setBackgroundResource(R.drawable.menu_selector);
// view.setBackgroundColor(Color.parseColor("#257CB5"));
((TextView) view).setTextColor(Color.WHITE);
}
} );
return view;
}
catch (InflateException e) {}
catch (ClassNotFoundException e) {}
}
return null;
}
});
}