溢出菜单未显示在 Activity 中,带有使用 TabLayout 实现的选项卡样式导航
Overflow menu is not showing in an Activity with a tab style navigation implemented with TabLayout
溢出菜单图标未显示在我的 Activity 选项卡样式导航布局中。这些选项卡是使用 ViewPager、Fragments 和 TabLayout 实现的。
溢出菜单显示在我的活动中,但没有选项卡,这让我认为溢出菜单未显示与我实现的选项卡导航有关。
这是我的 Activity 的基本模型:
public class MainActivity extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_layout);
initTabLayout();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_layout, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
public void initTabLayout(){
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setTabTextColors(Color.parseColor("#e3e8e7"), Color.parseColor("#7e7e7e"));
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setOffscreenPageLimit(4); //this stops the tabs from losing their data when you change tabs
//sets up adapter that contains the scrolling pageAdapter and the fragment which is displayed
viewPager.setAdapter(new ActivityTabAdapter(getSupportFragmentManager(),
MainActivity.this));
tabLayout.setupWithViewPager(viewPager);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
这是我的 FragmentPagerAdapter 实现:
public class ActivityTabAdapter extends FragmentPagerAdapter {
@Override
public Parcelable saveState() {
return super.saveState();
}
final int PAGE_COUNT = 4;
private String tabTitles[] = new String[]{"tab1", "tab2", "tab3", "tab4"};
private Context context;
public ActivityTabAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public Fragment getItem(int position) {
return PageFragment.newInstance(position + 1);
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
//These are fragments which hold the layouts of the tabs
public static class PageFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
Context mContext;
View tab1rootView;
View tab2Rootview;
View tab3RootView;
View tab4RootView;
Tab1 tab1;
Tab2 tab2;
Tab3 tab3;
Tab4 tab4;
private int mPage;
public static PageFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return fragment;
}
MenuInflater mInflater;
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
mInflater = inflater;
inflater.inflate(R.menu.menu_layout, menu);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
mInflater.inflate(R.menu.menu_layout, menu);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mContext = getActivity();
mPage = getArguments().getInt(ARG_PAGE);
Log.v("whichFrag", "" + getArguments().getInt(ARG_PAGE));
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
tab1rootView = inflater.inflate(R.layout.tab1_layout, container, false);
tab2Rootview = inflater.inflate(R.layout.tab2_layout, container, false);
tab3RootView = inflater.inflate(R.layout.tab3_layout, container, false);
tab4RootView = inflater.inflate(R.layout.tab4_layout, container, false);
int i = getArguments().getInt(ARG_PAGE);
if (getArguments().getInt(ARG_PAGE) == 1) { //if 1, then we inflate tab1rootView layout
tab1 = new Tab1(tab1rootView, getActivity());
tab1.initCardViews1();
return tab1rootView;
} else if (getArguments().getInt(ARG_PAGE) == 2) { //if 2, then we inflate tab2Rootview layout
tab2 = new Tab2(tab2Rootview, mContext);
return tab2Rootview;
} else if (getArguments().getInt(ARG_PAGE) == 3) { //if 3, then we inflate tab3RootView layout
tab3 = new Tab3(tab3RootView, mContext);
return tab3RootView;
} else { //if 4, then we inflate tab4RootView layout
tab4 = new Tab4(tab4RootView, mContext);
return tab4RootView;
}
}
}
}
这是我的 xml 菜单:
<menu 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"
tools:context="com.example.heavymagikhq.profilepageofficial.profileEditorJava.Profile_info_editor">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item
android:id="@+id/action_settings2"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"
android:visible="true" />
<item
android:id="@+id/action_settings1"
android:orderInCategory="100"
android:title="@string/action_settings"
android:icon="@drawable/ic_action_menu"
app:showAsAction="never"
android:visible="true" />
</menu>
我查看了 Whosebug 上的其他答案,但找不到答案。
如果您需要,我会 post 提供更多信息。
提前致谢。
尝试将 setHasOptionsMenu
移动到片段的 Oncreate()
方法并仅使用它 once.Also 不要在 OncreateOptionsMenu()
中膨胀菜单 twice.Inflate 并且更改 OnPrepareOptionsMenu()
中项目的可见性,而不是再次膨胀。
好的,我想通了,答案很简单。我不需要在 Fragments 的任何生命周期阶段使用 setHasOptionsMenu(true);
,根据我的经验,您必须将 app:showAsAction="never" 作为您的 xml 属性至少一个 xml 菜单项。
如果您在实现 TabLayout 的 Activity 中扩展 AppCompatActivity 并且您还在其中使用工具栏 Activity 然后获取对您的工具栏的引用并在将您的工具栏作为参数传递时调用 this.setSupportActionBar(toolbar);
。
它应该是这样的:
Toolbar toolbar = (Toolbar) findViewById(R.id.editProfileToolbar);
this.setSupportActionBar(toolbar);
并在 Activity 的 onCreate() 方法中调用它。
溢出菜单图标未显示在我的 Activity 选项卡样式导航布局中。这些选项卡是使用 ViewPager、Fragments 和 TabLayout 实现的。
溢出菜单显示在我的活动中,但没有选项卡,这让我认为溢出菜单未显示与我实现的选项卡导航有关。
这是我的 Activity 的基本模型:
public class MainActivity extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_layout);
initTabLayout();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_layout, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
public void initTabLayout(){
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setTabTextColors(Color.parseColor("#e3e8e7"), Color.parseColor("#7e7e7e"));
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setOffscreenPageLimit(4); //this stops the tabs from losing their data when you change tabs
//sets up adapter that contains the scrolling pageAdapter and the fragment which is displayed
viewPager.setAdapter(new ActivityTabAdapter(getSupportFragmentManager(),
MainActivity.this));
tabLayout.setupWithViewPager(viewPager);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
这是我的 FragmentPagerAdapter 实现:
public class ActivityTabAdapter extends FragmentPagerAdapter {
@Override
public Parcelable saveState() {
return super.saveState();
}
final int PAGE_COUNT = 4;
private String tabTitles[] = new String[]{"tab1", "tab2", "tab3", "tab4"};
private Context context;
public ActivityTabAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public Fragment getItem(int position) {
return PageFragment.newInstance(position + 1);
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
//These are fragments which hold the layouts of the tabs
public static class PageFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
Context mContext;
View tab1rootView;
View tab2Rootview;
View tab3RootView;
View tab4RootView;
Tab1 tab1;
Tab2 tab2;
Tab3 tab3;
Tab4 tab4;
private int mPage;
public static PageFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return fragment;
}
MenuInflater mInflater;
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
mInflater = inflater;
inflater.inflate(R.menu.menu_layout, menu);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
mInflater.inflate(R.menu.menu_layout, menu);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mContext = getActivity();
mPage = getArguments().getInt(ARG_PAGE);
Log.v("whichFrag", "" + getArguments().getInt(ARG_PAGE));
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
tab1rootView = inflater.inflate(R.layout.tab1_layout, container, false);
tab2Rootview = inflater.inflate(R.layout.tab2_layout, container, false);
tab3RootView = inflater.inflate(R.layout.tab3_layout, container, false);
tab4RootView = inflater.inflate(R.layout.tab4_layout, container, false);
int i = getArguments().getInt(ARG_PAGE);
if (getArguments().getInt(ARG_PAGE) == 1) { //if 1, then we inflate tab1rootView layout
tab1 = new Tab1(tab1rootView, getActivity());
tab1.initCardViews1();
return tab1rootView;
} else if (getArguments().getInt(ARG_PAGE) == 2) { //if 2, then we inflate tab2Rootview layout
tab2 = new Tab2(tab2Rootview, mContext);
return tab2Rootview;
} else if (getArguments().getInt(ARG_PAGE) == 3) { //if 3, then we inflate tab3RootView layout
tab3 = new Tab3(tab3RootView, mContext);
return tab3RootView;
} else { //if 4, then we inflate tab4RootView layout
tab4 = new Tab4(tab4RootView, mContext);
return tab4RootView;
}
}
}
}
这是我的 xml 菜单:
<menu 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"
tools:context="com.example.heavymagikhq.profilepageofficial.profileEditorJava.Profile_info_editor">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item
android:id="@+id/action_settings2"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"
android:visible="true" />
<item
android:id="@+id/action_settings1"
android:orderInCategory="100"
android:title="@string/action_settings"
android:icon="@drawable/ic_action_menu"
app:showAsAction="never"
android:visible="true" />
</menu>
我查看了 Whosebug 上的其他答案,但找不到答案。
如果您需要,我会 post 提供更多信息。
提前致谢。
尝试将 setHasOptionsMenu
移动到片段的 Oncreate()
方法并仅使用它 once.Also 不要在 OncreateOptionsMenu()
中膨胀菜单 twice.Inflate 并且更改 OnPrepareOptionsMenu()
中项目的可见性,而不是再次膨胀。
好的,我想通了,答案很简单。我不需要在 Fragments 的任何生命周期阶段使用 setHasOptionsMenu(true);
,根据我的经验,您必须将 app:showAsAction="never" 作为您的 xml 属性至少一个 xml 菜单项。
如果您在实现 TabLayout 的 Activity 中扩展 AppCompatActivity 并且您还在其中使用工具栏 Activity 然后获取对您的工具栏的引用并在将您的工具栏作为参数传递时调用 this.setSupportActionBar(toolbar);
。
它应该是这样的:
Toolbar toolbar = (Toolbar) findViewById(R.id.editProfileToolbar);
this.setSupportActionBar(toolbar);
并在 Activity 的 onCreate() 方法中调用它。