Android Studio 1.4 导航抽屉

Android Studio 1.4 Navigation Drawer

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

谢谢

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

在这种情况下,最好使用片段,对于每个导航项,在主 activity 中替换当前片段,这将很简单。请参阅 android 开发指南中的片段部分。

我遇到了同样的问题,但已经解决了。

按照以下步骤操作:

1.open“content_main.xml”文件位于“布局”文件夹中。

2.use 代码如下:

<?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>
  1. 转到“onNavigationItemSelected”方法:
   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;

您最好使用片段而不是 activity 在每个导航选项上添加内容。(在 Android Studio 中使用 Navigation Drawer Activity 选项创建应用程序后)。

content_main.xml(给这个相对布局分配id,Example:android:id="@+id/relative_layout_for_fragment")

<?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>

MainActivity.Java(你这样改片段)

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