我试图从一组片段中添加一个项目到选项菜单。
我已经创建了一个新的MenuFragment类,并扩展了我希望包含菜单项的片段。代码如下:
Java:
public class MenuFragment extends Fragment {
MenuItem fav;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
fav = menu.add("add");
fav.setIcon(R.drawable.btn_star_big_off);
}
}
科特林:
class MenuFragment : Fragment {
lateinit var fav: MenuItem
override fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
fav = menu.add("add");
fav.setIcon(R.drawable.btn_star_big_off);
}
}
由于某种原因,onCreateOptionsMenu似乎没有运行。
如果你需要一个菜单来刷新一个特定片段中的webview,你可以使用:
片段:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Add your menu entries here
inflater.inflate(R.menu.menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.exit:
System.exit(1);
break;
case R.id.refresh:
webView.reload();
break;
}
return true;
}
menu。xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/exit" android:title="Exit" android:icon="@drawable/ic_action_cancel" />
<item android:id="@+id/refresh" android:title="Refresh" android:icon="@drawable/ic_action_refresh" />
</menu>
除此之外,我还要补充一件事,以及它不适合我的原因。
这与Napster的答案相似。
Make sure your fragment's hosting activity extends AppCompatActivity, not FragmentActivity!
public class MainActivity extends AppCompatActivity {
}
From the Google Reference Documentation for FragmentActivity:
Note: If you want to implement an activity that includes an action bar, you should instead use the ActionBarActivity class, which is a subclass of this one, so allows you to use Fragment APIs on API level 7 and higher.
To update Napster's answer -- ActionBarActivity now being deprecated, use AppCompatActivity instead.
When using AppCompatActivity, also make sure you set "the activity theme toTheme.AppCompat or a similar theme" (Google Doc).
注意:android.support.v7.app。AppCompatActivity是android.support.v4.app的子类。FragmentActivity类(参见AppCompatActivity ref doc)。
我也遇到了同样的问题,但我认为最好总结并介绍让它工作的最后一步:
在Fragment的onCreate(Bundle savedInstanceState)方法中添加sethasoptionmenu (true)方法。
重写onCreateOptionsMenu(菜单菜单,MenuInflater膨胀器)(如果你想做一些不同的在你的片段的菜单)和onOptionsItemSelected(MenuItem项目)方法在你的片段。
在onOptionsItemSelected(MenuItem item) Activity的方法中,当菜单项操作将在onOptionsItemSelected(MenuItem item) Fragment的方法中实现时,确保返回false。
一个例子:
活动
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.activity_menu_item:
// Do Activity menu item stuff here
return true;
case R.id.fragment_menu_item:
// Not implemented here
return false;
default:
break;
}
return false;
}
片段
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
....
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Do something that differs the Activity's menu here
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.activity_menu_item:
// Not implemented here
return false;
case R.id.fragment_menu_item:
// Do Fragment menu item stuff here
return true;
default:
break;
}
return false;
}