我有一个其中包含ViewPager的FragmentActivity,并且需要屏幕顶部操作栏中的选项菜单。我添加到ViewPager中的片段中对public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
的调用从未被调用,因此从不创建选项菜单,并且不显示任何操作栏。
我已将setHasOptionsMenu(true);
添加到片段的public void onCreate(@Nullable Bundle savedInstanceState)
中。
我还尝试过将选项菜单直接添加到包含ViewPager的FragmentActivity中,但这似乎也不起作用。
public class MainActivity extends FragmentActivity {
private PagerAdaptor pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
setupPageViewer();
super.onCreate(savedInstanceState);
}
private void setupPageViewer() {
UIControlSurface cs = new UIControlSurface();
UIControlSurface cs2 = new UIControlSurface();
pager = new PagerAdaptor(getSupportFragmentManager());
pager.addPage(cs);
pager.addPage(cs2);
ViewPager v_pager = (ViewPager)super.findViewById(R.id.view_pager);
v_pager.setAdapter(pager);
v_pager.postInvalidate();
}
}
<android.support.constraint.ConstraintLayout 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:id="@+id/button_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
tools:context=".MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible" />
</android.support.constraint.ConstraintLayout>
public class UIControlSurface extends Fragment implements UIEditableListener{
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.ui_control_surface, container, false);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.i("Menu Inflation", "Here i am!");// never logged
inflater.inflate(R.menu.navi, menu);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.navi_enter_edit:
return true;
case R.id.navi_cancel:
return true;
}
return false;
}
}
<android.support.constraint.ConstraintLayout 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:id="@+id/button_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/add_button"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="52dp"
android:layout_height="54dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="+"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
在片段的onCreate方法中调用setHasOptionsMenu(true);
时,我希望菜单已创建,但是操作栏和选项菜单从不出现。片段按预期出现在ViewPager中,只是没有可见的选项菜单。