导航抽屉和底栏导航在同一个应用

时间:2017-11-21 10:14:50

标签: android

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

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,app正在运行。但是当我在Activity.xml中添加BottomNavigationView时,在logcat中显示crashed.nothing。

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

2 个答案:

答案 0 :(得分:0)

  

打开此布局

 layout="@layout/app_bar_main"
  

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

<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" />

答案 1 :(得分:0)

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

2-navigation.xml(您为BottomNavigation Bar创建的菜单)

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

  @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;
}