我正在使用Android Developer中的代码来显示ActionBar,但我无法让它工作。如果我点击 在ActionBar上的图标上没有响应,但单击文本BACK(在这种情况下)工作;然而 从xml文件中删除图标行(如下)一切正常! 那么我怎么能纠正这个呢?这是res / menu / main_activity_actions.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_help"
android:icon="@drawable/ic_action_help"
android:title="@string/action_help"
android:showAsAction="ifRoom" />
<item android:id="@+id/action_back"
android:title="@string/action_back"
android:showAsAction="ifRoom" />
</menu>
删除&#34;图标=&#34;行修复了一些事情,但我希望显示图标。 MainActivity(仅响应 to action_back clicked)和strings.xml文件如下所示:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id){
case R.id.action_help :
Toast.makeText(this, "Help clicked", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_back :
Toast.makeText(this, "Back clicked", Toast.LENGTH_SHORT).show();
return true;
default :
return super.onOptionsItemSelected(item);
}
}
<resources>
<string name="action_help">Help</string>
<string name="action_back">Back</string>
</resources>
答案 0 :(得分:0)
You have to include both this in your menu.xml file
appcompat v7
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
appcompat v7+
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res-auto"
答案 1 :(得分:0)
Use this code instead of yours and check whether its working or not.
Hope it works.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id){
case R.id.action_help :
Toast.makeText(this, "Help clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.action_back :
Toast.makeText(this, "Back clicked", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}