我想用操作栏显示我的菜单,但我的菜单不会显示,这是我的源代码:
public class EpolicyMainActivity extends TabActivity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
//hide title bar
BasicDisplaySettings.toggleTaskBar(EpolicyMainActivity.this, false);
//show status bar
BasicDisplaySettings.toggleStatusBar(EpolicyMainActivity.this, true);
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, LoginActivity.class);
spec = tabHost.newTabSpec("Login").setIndicator("",
res.getDrawable(R.drawable.epolicy_menu_xml_home))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, NABActivity.class);
spec = tabHost.newTabSpec("NAB").setIndicator("",
res.getDrawable(R.drawable.epolicy_menu_xml_nab))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, ContactActivity.class);
spec = tabHost.newTabSpec("Contact").setIndicator("",
res.getDrawable(R.drawable.epolicy_menu_xml_contact))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, AgenActivity.class);
spec = tabHost.newTabSpec("Agen").setIndicator("",
res.getDrawable(R.drawable.epolicy_menu_xml_agen))
.setContent(intent);
tabHost.addTab(spec);
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++){
tabHost.getTabWidget().getChildAt(i).setPadding(0,0,0,0);
tabHost.getTabWidget().getChildTabViewAt(i).setBackgroundDrawable(null);
}
tabHost.setCurrentTab(0);
}
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.menu_bantuan:
Intent itAbout = new Intent(EpolicyMainActivity.this, EpolicyBantuan.class);
itAbout.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(itAbout);
break;
case R.id.menu_exit:
dialogExit();
break;
case R.id.menu_logout:
dialogSignOut();
break;
}
return true;
}
public void dialogSignOut()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Apakah anda ingin sign-out?")
.setCancelable(false)
.setPositiveButton("Ya", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent itSignOut = new Intent(EpolicyMainActivity.this, LoginActivity.class);
itSignOut.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(itSignOut);
finish();
}
})
.setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
public void dialogExit()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Apakah anda ingin keluar?")
.setCancelable(false)
.setPositiveButton("Ya", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent itSplashEnd = new Intent(EpolicyMainActivity.this, SplashOutActivity.class);
itSplashEnd.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
itSplashEnd.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(itSplashEnd);
finish();
}
})
.setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
@Override
public void onBackPressed() {
dialogExit();
}
这是我的menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_bantuan"
android:title="@string/menu_bantuan"
android:orderInCategory="100"
android:showAsAction="ifRoom|withText"/>
<item android:id="@+id/menu_exit"
android:title="@string/menu_exit"
android:orderInCategory="100"
android:showAsAction="ifRoom|withText" />
<item android:id="@+id/menu_logout"
android:title="@string/menu_logout"
android:orderInCategory="100"
android:showAsAction="ifRoom|withText" />
在我的主要版面中,我正在使用标题,这是否会影响我的菜单,所以我的菜单不会显示或其他任何内容?
答案 0 :(得分:0)
我假设你应该先打电话给超级。
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
我假设你的menu.xml有一个合适的菜单结束标记。你有这个xml文件存储在res / menu文件夹中吗?你能验证是否调用了onCreateOptionsMenu?
在Android 2.3及更低版本中,您必须按菜单按钮,而在以后的发行版中,它应显示在标题栏中。由于您使用的是TabActivity,我假设您正在为Android 2.3编写应用程序。
此问题可能对onMenuItemSelected()方法有用: Merge TabActivity menu with contained Activities menus
对于您内的活动TabActivity:How to create an optionsMenu in an Android's TabActivity
也许当您按下菜单按钮时,将调用您所在的特定选项卡活动的选项菜单(例如LogInActivity),而不是其父项的选项菜单。尝试将此代码放在每个子活动中:
public boolean onCreateOptionsMenu(Menu menu)
{
return getParent().onCreateOptionsMenu(menu);
}
答案 1 :(得分:0)
如果设备没有硬件菜单按钮并且您设置了targetSDK版本&gt; 10个用户无法使用菜单。作为短期解决方案,您可以设置targetSDK = 10作为长期解决方案考虑使用ActionBar http://developer.android.com/guide/topics/ui/actionbar.html