Android Studio 1.4导航抽屉

Android Studio 1.4导航抽屉,android,android-studio,navigation-drawer,Android,Android Studio,Navigation Drawer,我是Android应用开发新手。今天我尝试将我的应用程序升级到android新材料设计。所以我使用了Android studio(1.4)导航抽屉活动。问题是我无法理解如何使用导航栏在我的活动之间导航。它与我看到的在线教程不同。它不使用碎片。 我可以更改名称、图标。。等等,问题是我无法理解如何使用导航抽屉在活动之间导航 多谢各位 public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation v

我是Android应用开发新手。今天我尝试将我的应用程序升级到android新材料设计。所以我使用了Android studio(1.4)导航抽屉活动。问题是我无法理解如何使用导航栏在我的活动之间导航。它与我看到的在线教程不同。它不使用碎片。 我可以更改名称、图标。。等等,问题是我无法理解如何使用导航抽屉在活动之间导航

多谢各位

 public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.

    int id = item.getItemId();

    if (id == R.id.nav_camara) {


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

    } 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) {

    }

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

在这种情况下,最好使用片段,对于每个导航项目,在您的主要活动中替换当前片段,这样做会很简单。请参阅《android开发指南》中的片段部分。

我也遇到了同样的问题,但得到了修复

按照以下步骤操作:

1.打开“布局”文件夹中的“content_main.xml”文件

2.使用以下代码:

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/app_bar_main"
        tools:context=".MainActivity">    

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/mainFrame">

       </FrameLayout>

     </RelativeLayout>

您可以更好地使用fragment not activity在每个导航选项上添加内容。(在Android Studio中使用导航抽屉活动选项创建应用程序后)

content_main.xml(将id分配给此相对布局,例如:android:id=“@+id/relative_layout_for_fragment”)


事实上,他问导航抽屉有活动,但你的代码有片段。
   public boolean onNavigationItemSelected(MenuItem item) {

        int id = item.getItemId();
        Fragment fragment = new YourFragment();

        if (id == R.id.nav_camara) {

            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.mainFrame, fragment);
            ft.commit();

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


        } 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) {


        }

        //Close Drawer After Action
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);

        return true;
<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.example.root.netutility.MainActivity"
        tools:showIn="@layout/app_bar_main"
        android:id="@+id/relative_layout_for_fragment">

        <!--<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!" />-->
    </RelativeLayout>
   @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        Fragment fragment = null;     
        if (id == R.id.ping_tab) {          
            fragment = new FirstFragment();
            toolbar.setTitle("First");
        } else if (id == R.id.traceroute_tab) {
            fragment = new SecondFragment();
            toolbar.setTitle("Second"); // if you wan to change title
        }else if (id == R.id.nav_share) {
            fragment = new ThirdFragment();
        } else if (id == R.id.nav_send) {
            fragment = new FourthFragment();
        }
        if(fragment != null) {
            // update the main content by replacing fragments
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.relative_layout_for_fragment, fragment).commit();
        }

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