3

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

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

这是我的活动的基本模型:

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上查看了其他答案,但找不到答案。

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

提前致谢。

4

2 回答 2

3

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

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

它应该是这样的:

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

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

于 2016-03-30T11:46:48.047 回答
0

尝试移动setHasOptionsMenuOncreate()片段的方法并只使用一次。也不要两次充气菜单。充气一次OncreateOptionsMenu()并更改项目的可见性,OnPrepareOptionsMenu()而不是再次充气。

于 2016-03-30T11:38:56.077 回答