我的目标是在所有活动中共享操作栏。所以我创建了一个名为“MyActionBarActivity”的基础类,如下所示“
public class MyActionBarActivity extends ActionBarActivity {
public final static int TL = Toast.LENGTH_LONG;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
Toast.makeText(getApplicationContext(), "I am settings", TL).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
和其他活动从这个类继承来共享操作栏。
public class Activity1 extends MyActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
}}
public class Activity2 extends MyActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
}}
这是实现操作栏的好方法吗?非常感谢您的建议,并在此先感谢!