【问题标题】:Android Studio 1.4 Navigation DrawerAndroid Studio 1.4 导航抽屉
【发布时间】:2015-10-05 15:16:12
【问题描述】:

我是 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 android-studio navigation-drawer


    【解决方案1】:

    我有同样的问题,但得到了解决。

    按照以下步骤操作:

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

    【讨论】:

    • 其实他用activites问Navigation Drawer,但是你的代码是用Fragments的。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      您可以更好地使用片段而不是活动在每个导航选项上添加内容。(在 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;
          }
      

      【讨论】:

        猜你喜欢
        • 2020-01-15
        • 1970-01-01
        • 2020-12-11
        • 2016-04-18
        • 2016-12-22
        • 2016-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多