如何在导航抽屉中使用Fragment?

问题描述 投票:0回答:3

我试过了。我不成功。我正在写我的步骤。如果有人可以帮助我。

  1. 使用Android Studio创建新项目并选择“导航抽屉活动”
  2. 我将FrameLayout放在主要活动中,如下所示 <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:openDrawer="start"> <include layout="@layout/app_bar_vshome" android:layout_width="match_parent" android:layout_height="match_parent" /> <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" app:headerLayout="@layout/nav_header_vshome" app:menu="@menu/activity_vshome_drawer" /> <!-- Framelayout to display Fragments --> <FrameLayout android:id="@+id/frame_container" android:layout_width="match_parent" android:layout_height="match_parent" /> </android.support.v4.widget.DrawerLayout>
  3. 我使用v4.app.Fragment创建了新类,如下所示 public class VSAllTopics extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_all_topics, container, false); } }
  4. 我做了Fragment Manager,如下所示, qazxsw poi
  5. 调用** public boolean onNavigationItemSelected(MenuItem item)** public class FragmentManager extends Fragment { }

我研究了一点,但我无法成功。它不是在呼唤。

如果我通过Intent调用它,它会删除NAVIGATION;(

如何以正确的方式使用侧面菜单。

谢谢。

android android-fragments navigation-drawer
3个回答
0
投票

这是您要查找的链接:

android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction ft = fragmentManager.beginTransaction(); if (id == R.id.nav_camera) { ft.replace(R.id.frame_container, new VSAllTopics(), "VS_ALL").commit();


0
投票

您的导航视图应位于布局中的FrameLayout之后。

Android按照它在xml中显示的顺序绘制每个视图。由于你的FrameLayout的宽度和高度都有match_parent,因此android会将它绘制在NavigationView上方。

以下是解决方案的代码:

Fragment Navigation Drawer

0
投票

建议在Drawer布局中使用navigationView。最简单的选择是使用android studio<include layout="@layout/app_bar_vshome" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- Framelayout to display Fragments --> <FrameLayout android:id="@+id/frame_container" android:layout_width="match_parent" android:layout_height="match_parent" /> <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" app:headerLayout="@layout/nav_header_vshome" app:menu="@menu/activity_vshome_drawer" /> 提供的导航Activity

然后你可以编辑项目

activity_main_drawer.xml

并添加您自己的项目和他们各自的图标。

您也可以编辑导航标题视图

navigation_header_main.xml

为了在您的Activity中的导航抽屉中使用Fragment

使用它像这样:

enter image description here
© www.soinside.com 2019 - 2024. All rights reserved.