【发布时间】:2014-09-25 05:34:36
【问题描述】:
我将以下 menu.xml 用于操作栏菜单,并且它在我的主要活动中运行良好
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="@+id/action_refresh"
android:icon="@drawable/ic_drawer"
android:orderInCategory="0"
android:title="navigation"
app:showAsAction="always"/>
<item
android:id="@+id/action_settings"
android:icon="@drawable/settings"
android:orderInCategory="0"
android:title="Settings"
app:showAsAction="always">
</item>
但是当我在其他活动菜单上使用相同的 menu.xml 时,菜单图标不会出现。这是我的主要活动的快照
现在这里是我的其他活动的快照。 为什么我会遇到这个问题,请让我知道您对此的反馈。
这是我的 onCreateOptionsMenu 代码
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.test,menu);
return true;
}
我将这段代码用于这两项活动。
这是我的 SecondActivity 的代码
package com.example.actionbar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.test, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_refresh) {
Toast.makeText(this, "navigation selected", Toast.LENGTH_SHORT)
.show();
}
if(id == R.id.action_settings){
Toast.makeText(this, "settings selected", Toast.LENGTH_SHORT)
.show();
}
return super.onOptionsItemSelected(item);
}
}
【问题讨论】:
-
您能否为这两个活动发布您的 onCreateOptionsMenu。
-
发布
onOptionsItemSelected方法。如果你还没有覆盖这个方法,那么你应该这样做。 -
我看不出有什么问题,你能发布第二个活动的完整代码吗?
-
是的,我已经发布了第二个活动的完整代码,在第一个活动中它可以找到但在第二个活动中我仍然面临同样的问题
-
我注意到你在一个地方使用 R.menu.action_menu,而在另一个地方你正在使用 R.menu.test。会不会是个问题?
标签: android xml android-layout android-activity