选项卡上的Android导航抽屉

我正在使用支持库中提供的新导航抽屉。 使用抽屉和标签时,抽屉菜单将显示在标签下方,如下所示。 如何确保选项卡上显示抽屉菜单。 (它应该显示抽屉菜单,好像没有标签)

没有标签的抽屉菜单 在此处输入图像描述

带标签的抽屉菜单

在此处输入图像描述

Solutions Collecting From Web of "选项卡上的Android导航抽屉"

我得到了同样的问题,我从Roman Nurik(Android团队)得到的答案是导航抽屉不应该与Action Bar选项卡一起使用。

可以在此处find讨论: https : //plus.google.com/u/1/116515063230772720916/posts/8dWEkFcbTFX

创建一个单独的片段,说TabsFragment,并在主活动的片段容器中添加片段。

可以使用以下代码在选项卡片段内添加选项卡。

private FragmentTabHost mTabHost; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mTabHost = new FragmentTabHost(getActivity()); mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.fragment1); mTabHost.addTab(mTabHost.newTabSpec("TabA").setIndicator("TabA"), TabA.class, null); mTabHost.addTab(mTabHost.newTabSpec("TabB").setIndicator("TabB"), TabB.class, null); return mTabHost; } 

这里,TabA和TabB是选项卡的单独片段。 您可以在相应的片段中实现相应选项卡的function。 参考: http : //developer.android.com/reference/android/support/v4/app/FragmentTabHost.html

我不确定Action Bar标签,但你可以使用Pager Tab Strip结合导航抽屉来获得类似Google Play音乐的导航模型,看看我的post

这是我正在使用的解决方案(完美地工作):我使用框架布局来交换片段,选项卡式布局是基础片段上的选项卡。

activity_main.xml中

             

您可以隐藏View负责渲染选项卡manualy:

 public static void setActionBarNavigationVisibility(Activity activity, boolean visible) { // --- If the Tabs are BELOW ActionBar (narrow screens) --- int actionViewResId = Resources.getSystem().getIdentifier("action_bar_container", "id", "android"); // @see http://stackoverflow.com/questions/20023483/how-to-get-actionbar-view View actionBarContainer = activity.findViewById(actionViewResId); // returns instance of com.android.internal.widget.ActionBarContainer (inaccessible) try { Field mTabContainerField = actionBarContainer.getClass().getDeclaredField("mTabContainer"); if (mTabContainerField != null) { mTabContainerField.setAccessible(true); View mmTabContainer = (View) mTabContainerField.get(actionBarContainer); if (mmTabContainer != null) mmTabContainer.setVisibility(visible ? View.VISIBLE : View.GONE); // use GONE, so the mTabContainer below ActionBar does not take space in layout } } catch (Exception e) { // TODO handle exception } } 

只需从ActionBarDrawerToggle的实现中调用此方法,如下所示:

 public void onDrawerSlide(View drawerView, float slideOffset) { super.onDrawerSlide(drawerView, slideOffset); if (slideOffset == 0) { // 0 = drawer is closed setActionBarNavigationVisibility(activity, true); //show Tabs when Drawer is closed } } public void onDrawerStateChanged(int newState) { super.onDrawerStateChanged(newState); //hides Tabs right after Drawer starts opening if (DrawerLayout.STATE_DRAGGING == newState || DrawerLayout.STATE_SETTLING == newState) { setActionBarNavigationVisibility(activity, false); } } 

如果您在窄屏幕上使用ActionBar.Tabs,此解决方案将正常工作(Tabs会自动显示在ActionBar下方)。 但是如果你想处理所有其他导航场景(NavigationMode:List,CustomNavigationView或Tabs直接在ActionBar中显示),第一种方法需要处理所有这些可能性:

 public static void setActionBarNavigationVisibility(Activity activity, boolean visible) { try { /* 1. --- If the navigation items are showing in ActionBar directly. We have 3 options Spinner, Tabs, and CustomNav --- (When Tabs are showing BELOW ActionBar, is handled at the end) */ int actionViewResId = Resources.getSystem().getIdentifier("action_bar", "id", "android"); // @see http://stackoverflow.com/questions/20023483/how-to-get-actionbar-view View actionBarView = activity.findViewById(actionViewResId); // returns instance of com.android.internal.widget.ActionBarView (inaccessible) if (actionBarView != null) { int visibility = visible ? View.VISIBLE : View.INVISIBLE; // not GONE, so it still takes space in ActionBar layout // handle tabs navigation Field mTabScrollViewField = actionBarView.getClass().getDeclaredField("mTabScrollView"); if (mTabScrollViewField != null) { mTabScrollViewField.setAccessible(true); View mTabScrollView = (View) mTabScrollViewField.get(actionBarView); // instance of com.android.internal.widget.ScrollingTabContainerView (inaccessible) if (mTabScrollView != null) mTabScrollView.setVisibility(visibility); } // handle Spinner navigation Field mSpinnerField = actionBarView.getClass().getDeclaredField("mSpinner"); // resp. mListNavLayout if (mSpinnerField != null) { mSpinnerField.setAccessible(true); View mSpinner = (View) mSpinnerField.get(actionBarView); // instance of android.widget.Spinner if (mSpinner != null) mSpinner.setVisibility(visibility); } // handle Custom navigation Field mCustomNavViewField = actionBarView.getClass().getDeclaredField("mCustomNavView"); // resp. mListNavLayout if (mCustomNavViewField != null) { mCustomNavViewField.setAccessible(true); View mCustomNavView = (View) mCustomNavViewField.get(actionBarView); if (mCustomNavView != null) mCustomNavView.setVisibility(visibility); } } // 2. --- If the Tabs are BELOW ActionBar (narrow screens) --- ViewParent actionBarContainer = actionBarView.getParent(); // parent of ActionBarView is com.android.internal.widget.ActionBarContainer (inaccessible) Field mTabContainerField = actionBarContainer.getClass().getDeclaredField("mTabContainer"); if (mTabContainerField != null) { mTabContainerField.setAccessible(true); View mmTabContainer = (View) mTabContainerField.get(actionBarContainer); if (mmTabContainer != null) mmTabContainer.setVisibility(visible ? View.VISIBLE : View.GONE); // now use GONE, so the mTabContainer below Actionbar does not take space in layout } } catch (Exception ex) { // TODO Handle exception... } } 

WordPress Android版本有你所说的问题的实现。

他们开发了一个名为“Horizo​​ntalTabView”的模仿版TabView。 为什么他们这样做与你(也是我)需要的完全相同。

WordPress github: https : //github.com/WordPress/WordPress

相关来源:

  • Horizo​​ntalTabView.java
  • WPActionBarActivity.java
  • RES / theme_browser_activity.xml

希望这个提示有帮助。

我认为现在问题已经太迟了,但仍然存在。 如果您已经在使用导航抽屉并想要导航选项卡,请在项目文件夹中包含这两个文件:

1)SlidingTabLayout.java http://developer.android.com/samples/SlidingTabsBasic/src/com.example.android.common/view/SlidingTabLayout.html

2)SlidingTabStrip.java http://developer.android.com/samples/SlidingTabsBasic/src/com.example.android.common/view/SlidingTabStrip.html

在导航抽屉片段中,包括以下内容:

 private SlidingTabLayout mSlidingTabLayout; private SamplePagerAdapter_tasks mPagerAdapter; private ViewPager mViewPager; public void onViewCreated(View view, Bundle savedInstanceState) { // BEGIN_INCLUDE (setup_viewpager) // Get the ViewPager and set it's PagerAdapter so that it can display items //fragment manager of a fragment,Return a private FragmentManager for placing and managing Fragments inside of this Fragment. mPagerAdapter = new SamplePagerAdapter_tasks(getChildFragmentManager(),Titles of Tabs(array of string),Numboftabs (integer)); mViewPager = (ViewPager) view.findViewById(R.id.viewpager); mViewPager.setAdapter(mPagerAdapter); // Give the SlidingTabLayout the ViewPager, this must be done AFTER the ViewPager has had // it's PagerAdapter set. mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs); mSlidingTabLayout.setDistributeEvenly(true); // this helps to all the tabs on screen to take equal spaces (eg. if 3 tabs each would take one-third of the total space on the screen) mSlidingTabLayout.setViewPager(mViewPager); // END_INCLUDE (setup_slidingtablayout) } 

希望能帮助到你!!

在我的AppCompatActivity中,我添加了这个:

 DrawerLayout drawerLayoutMenu =(DrawerLayout) findViewById(R.id.drawer_layout_menu); RelativeLayout mDrawerPane =(RelativeLayout) findViewById(R.id.drawerPane); 

我在工具栏菜单图标上调用它

 if (drawerLayoutMenu.isDrawerOpen(mDrawerPane)) { drawerLayoutMenu.closeDrawer(mDrawerPane); } else { drawerLayoutMenu.openDrawer(mDrawerPane); } 

xml文件

               

Actionbar Navigaton Drawer和SwipeTabs无法同时使用。 您应该使用Actionbar和swipetabs通过简单的Tabhost实现Navigation Drawer。 您可以将Tabhost用于选项卡,并将片段用于每个选项卡的内部视图。 应通过viewpager使用片段来提供滚动/滑动效果。 通过他们的方法相互连接选项卡和viewpager