我正在使用支持库中提供的新导航抽屉。将抽屉与选项卡一起使用时,抽屉菜单将显示在选项卡下方,如下所示。我如何确保抽屉菜单显示在选项卡上。(它应该显示抽屉菜单,就好像没有标签一样)
没有标签的抽屉菜单
带标签的抽屉菜单
我正在使用支持库中提供的新导航抽屉。将抽屉与选项卡一起使用时,抽屉菜单将显示在选项卡下方,如下所示。我如何确保抽屉菜单显示在选项卡上。(它应该显示抽屉菜单,就好像没有标签一样)
没有标签的抽屉菜单
带标签的抽屉菜单
我遇到了同样的问题,我从 Roman Nurik(Android 团队)那里得到的答案是导航抽屉不应该与操作栏选项卡一起使用。
可以在这里找到讨论: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 是选项卡的单独片段。您可以在各自的片段中实现各自选项卡的功能。参考:http: //developer.android.com/reference/android/support/v4/app/FragmentTabHost.html
我不确定 Action Bar 选项卡,但您可以结合使用 Pager Tab Strip 和 Navigation Drawer 来获得类似于 Google Play Music 的导航模型,看看我的帖子
这是我正在使用的解决方案(效果很好):我正在使用框架布局来交换片段,而选项卡式布局是基本片段上的选项卡。
activity_main.xml
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--<include-->
<!--android:id="@+id/toolbar"-->
<!--layout="@layout/toolbar"/>-->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
scrol
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</FrameLayout>
<ListView
android:id="@+id/navList"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:background="#ffeeeeee"/>
</android.support.v4.widget.DrawerLayout>
您可以手动隐藏负责渲染选项卡的视图:
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(选项卡自动显示在 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 版本已实现您所说的问题。
他们开发了一个名为“HorizontalTabView”的模拟版 TabView。他们为什么这样做与您(也是我)需要的完全一样。
WordPress github: https ://github.com/WordPress/WordPress
相关来源:
希望这个提示有所帮助。
我相信自从提出问题以来为时已晚,但仍然如此。如果您已经在使用导航抽屉并想要导航选项卡,请在项目文件夹中包含两个文件:
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文件
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout_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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</FrameLayout>
<!--for menu-->
<RelativeLayout
android:id="@+id/drawerPane"
android:layout_width="270dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#fff"
android:layout_gravity="left|start">
<!--you can add a ListView-->
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
Actionbar Navigaton Drawer 和 SwipeTabs 不能同时使用。您应该通过简单的 Tabhosts 使用 Actionbar 和 swipetabs 来实现 Navigation Drawer。您可以将 Tabhost 用于选项卡,并将片段用于每个选项卡的内部视图。片段应该通过 viewpager 来提供滚动/滑动效果。通过它们的方法将选项卡和 viewpager 相互连接
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabHost
android:id="@+id/tabHost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TabWidget>
</HorizontalScrollView>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/viewPager_home"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
</TabHost>