Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
Log.i("frag", "onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
final View view = inflater.inflate(R.layout.fragment_initiate_chat, container, false);
bindViews(view);
setUpClickListener();
setUpCategories(view);
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//menu.clear();
inflater.inflate(R.menu.menu_inititate_chat, menu);
Log.i("frag", "onCreateOptionsMenu");
//super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.i("frag", "onOptionsItemSelected");
final int itemId = item.getItemId();
switch (itemId) {
case R.id.send:
sendIssue();
break;
}
return super.onOptionsItemSelected(item);
}
我无法为我的片段设置菜单。我使用了 setHasOptionsMenu(true); 但它仍然没有任何区别。
我的片段 xml 布局。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bgGrey"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:navigationIcon="@drawable/ic_close_black_24dp"
app:popupTheme="@style/Theme.AppCompat.NoActionBar"
app:titleTextColor="@color/toolbarTextColor" />
<Spinner
android:id="@+id/categories"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>`
菜单项 menu_inititate_chat.xml
`<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/send"
android:orderInCategory="100"
android:title="@string/send"
android:icon="@drawable/send"
app:showAsAction="always" />
</menu>`
包含此片段的活动不包含它自己的工具栏。