Android Studio 1.4 导航抽屉

7
我是一名新手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;
}
3个回答

7

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

按照以下步骤进行操作:

1.打开“layout”文件夹中的“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>

前往“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;

1
实际上,他要求带有活动的导航抽屉,但是您的代码使用片段。 - oneNiceFriend

0
在这种情况下,最好使用片段,对于每个导航项目,在主活动中替换当前片段,这将很容易实现。请参阅Android Dev Guide中的片段部分。

0

在使用Android Studio中的导航抽屉活动选项创建应用程序后,您可以更好地使用片段而不是活动来添加每个导航选项上的内容。

content_main.xml(为此相对布局分配ID,例如: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;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接