【发布时间】:2014-04-04 18:34:56
【问题描述】:
我想在我的应用程序中使用操作栏。到目前为止,我能够从支持库中添加操作栏。现在我想将项目添加到我的操作栏。我希望项目的图标显示在我的操作栏中,所以我做了以下操作:
首先我创建了 menu.xml 文件
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_locate"
android:icon="@drawable/ic_action_location_found"
android:title="@string/locate"
android:showAsAction="always" />
<!-- Settings, should always be in the overflow -->
我将这些函数添加到我的 mainActivity
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_locate:
Toast.makeText(this,"locate is selected",Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
当我运行我的应用程序时,我只看到操作栏的标题,定位图标不存在。当我触摸手机上的选项按钮时,会出现一个仅在其中定位的列表。我需要的是在操作栏的右上角出现定位图标。谁能告诉我我做错了什么以及为什么它没有出现在操作栏的右上角??
【问题讨论】:
-
你使用 AppCompat 吗?
-
就用这个吧:@Override public boolean onCreateOptionsMenu(Menu menu) { return getMenuInflater().inflate(R.menu.menu, menu); }