我在使用 ActionBarSherlock
库时尝试膨胀 ContextMenu
时遇到类型问题,如您所知,该库最终模仿/实现了 Android 支持库。
有一个SherlockFragmentActivity
,它设置布局内容,并且该内容中存在两个 fragment 。这些 fragment 之一是 SherlockListFragment
。在 SherlockListFragment
的 onCreate
中,我调用注册 ContextMenu
。
registerForContextMenu(getListView());
问题源于尝试膨胀菜单。
listView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
MenuInflater inflater = getSherlockActivity().getSupportMenuInflater();
inflater.inflate(R.menu.lot_menu, contextMenu);
}
});
由于方法参数中指定的类型,我无法调用 inflate
,它预计为 com.actionbarsherlock.view.Menu
但传入的类型是android.view.ContextMenu
.
我似乎遗漏了一些东西,因为在操作栏中膨胀Menu
是微不足道的,但是ContextMenu
在使用支持框架时似乎会带来问题。
如何正确注册以根据需要使用支持框架并随后扩展 ContextMenu
?
最佳答案
尝试使用 getMenuInflater()
而不是 getSupportMenuInflater()
来膨胀为 ContextMenu
。
关于android - 使用支持库时如何在 ListView 中提供 ContextMenu?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14468209/