gpt4 book ai didi

android - 溢出菜单未显示在具有使用 TabLayout 实现的选项卡样式导航的 Activity 中

转载 作者:可可西里 更新时间:2023-11-01 11:40:36 27 4
gpt4 key购买 nike

溢出菜单图标未显示在我的带有选项卡样式导航布局的 Activity 中。这些选项卡是使用 ViewPager、Fragments 和 TabLayout 实现的。

溢出菜单显示在我的 Activity 中,它没有选项卡,这让我认为未显示的溢出菜单与我实现的选项卡导航有关。

这是我的 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>

我查看了 stackoverflow 上的其他答案,但找不到答案。

如果您需要,我会发布更多信息。

提前致谢。

最佳答案

好吧,我想通了,答案很简单。我不需要在 fragment 的任何生命周期阶段使用 setHasOptionsMenu(true);,根据我的经验,您至少必须将 app:showAsAction="never"作为 xml 属性您的 xml 菜单项之一。

如果您在实现 TabLayout 的 Activity 中扩展 AppCompatActivity,并且还在该 Activity 中使用 Toolbar,那么获取对您的 Toolbar 的引用并调用 this.setSupportActionBar(toolbar);同时将您的工具栏作为参数传递。

应该是这样的:

 Toolbar toolbar = (Toolbar) findViewById(R.id.editProfileToolbar);
this.setSupportActionBar(toolbar);

并在您的 Activity 的 onCreate() 方法中调用它。

关于android - 溢出菜单未显示在具有使用 TabLayout 实现的选项卡样式导航的 Activity 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36306741/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com