本文介绍了ListFragment - 如何添加一个菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个ListFragment,我不得不添加一个菜单。这是我的code:
listuser_menu:
I have a ListFragment and I have to add a menu. This is my code:listuser_menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/any_option"
android:title="In Context Menu" />
</menu>
我ListFragment:
My ListFragment:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
View mFooterView;
// We need to use a different list item layout for devices older than Honeycomb
int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
android.R.layout.simple_list_item_activated_1:android.R.layout.simple_list_item_1;
if(getListAdapter()==null){
// init adapter
adapter=new UserArrayAdapter(getActivity(),
MOBILE_OS);
}
else{
adapter.notifyDataSetChanged();
}
// set adapter
registerForContextMenu(getListView());
setListAdapter(adapter);
}
@Override
public void onCreateContextMenu(final ContextMenu menu, final View v,
final ContextMenuInfo menuInfo){
menu.clear();
super.onCreateContextMenu(menu, v, menuInfo);
final MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.listuser_menu, menu);
}
但我看不到我的menu..why?如何Register选项菜单中选择?谢谢!
But i can't see my menu..why? How register option menu selected? Thanks!
推荐答案
添加菜单片段是可能的,请使用以下code在你的片段:
Adding menus to fragment is possible, use the following code in your Fragment:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.listuser_menu, menu);
}
使用setHasOptionsMenu将让您的片段显示一个菜单。
Using setHasOptionsMenu will allow your fragment to show a menu.
这篇关于ListFragment - 如何添加一个菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!