Android 同一应用程序中的导航抽屉和底部栏导航

Android 同一应用程序中的导航抽屉和底部栏导航,android,Android,我正在尝试在我的应用程序中使用导航抽屉和底部栏导航。因此,我首先创建了导航活动。然后我尝试将底部栏导航添加到同一活动中,如下所示 内部onCreate BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); Activity.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res

我正在尝试在我的应用程序中使用导航抽屉和底部栏导航。因此,我首先创建了导航活动。然后我尝试将底部栏导航添加到同一活动中,如下所示

内部
onCreate

BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
Activity.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    >
    <include
        layout="@layout/app_bar_main"
        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_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>
    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/navigation" />
</RelativeLayout>

在Activity.xml中没有
BottomNavigationView
,应用程序正在工作。但当我在Activity.xml中添加
BottomNavigationView
时,应用程序崩溃。logcat中没有显示任何内容

如何在同一活动中同时使用底部导航栏导航和导航抽屉

打开此布局

然后在该布局中添加底部导航视图小部件


底部导航和导航抽屉都使用OnNavigationItemSelectedListener从一个片段切换到另一个片段。 现在,在您的情况下,您将有两个菜单 1-activity_home_drawer.xml

2-navigation.xml(为底部导航栏创建的菜单)

因此,在方法内部,必须为两个菜单编写逻辑

  @Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Navigation Drawer Menus below.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action

        selectedFragment = new Batteries();
        bottomNavigationView.setVisibility(View.VISIBLE);

    } else if (id == R.id.nav_gallery) {
        selectedFragment = new Offers();
        bottomNavigationView.setVisibility(View.INVISIBLE);
    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }
    // Bottom Navigation Menus below
    else if (id == R.id.navigation_dashboard){
        selectedFragment = new Batteries();
    }else if (id == R.id.navigation_home){
        selectedFragment = new BatteryReport();
    }else if (id == R.id.navigation_notifications){
        selectedFragment = new ScanCode();
    }

    getSupportFragmentManager().beginTransaction()
            .replace(R.id.frameLayout, selectedFragment).commit();

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
<android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/navigation" />
  @Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Navigation Drawer Menus below.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action

        selectedFragment = new Batteries();
        bottomNavigationView.setVisibility(View.VISIBLE);

    } else if (id == R.id.nav_gallery) {
        selectedFragment = new Offers();
        bottomNavigationView.setVisibility(View.INVISIBLE);
    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }
    // Bottom Navigation Menus below
    else if (id == R.id.navigation_dashboard){
        selectedFragment = new Batteries();
    }else if (id == R.id.navigation_home){
        selectedFragment = new BatteryReport();
    }else if (id == R.id.navigation_notifications){
        selectedFragment = new ScanCode();
    }

    getSupportFragmentManager().beginTransaction()
            .replace(R.id.frameLayout, selectedFragment).commit();

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}