2016-11-19 80 views
0

我已經在空白活動(Android Studio)上創建了整個項目。我想添加一個導航抽屜而不妨礙我以前的代碼。是否有一種方法可以將導航抽屜添加到空白活動中,如果是的話請引導。添加導航抽屜以空白活動

+2

檢查此鏈接https://developer.android.com/training/implementing-navigation/nav-drawer.html – Swr7der

+0

@ Swr7der謝謝你,有沒有可能,如果你可以發送源代碼,如果完全好你不能 – Anonymous

+0

這是,但你應該嘗試自己學習和編碼。這對你有好處。如果在此之後,你面臨問題,然後在這裏發帖。有同行可以幫助你。快樂編碼:) – Swr7der

回答

1

第1步:將導航視圖包含到您的xml文件中,這將通過創建時的yor活動進行膨脹。

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

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

第二步:初始化觀點

private NavigationView navigationView; 
private DrawerLayout drawer; 

第三步:發送事件的抽屜關閉和打開

ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.openDrawer, R.string.closeDrawer) { 

     @Override 
     public void onDrawerClosed(View drawerView) { 
      // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank 
      super.onDrawerClosed(drawerView); 
     } 

     @Override 
     public void onDrawerOpened(View drawerView) { 
      // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank 
      super.onDrawerOpened(drawerView); 
     } 
    }; 

    //Setting the actionbarToggle to drawer layout 
    drawer.setDrawerListener(actionBarDrawerToggle); 

    //calling sync state is necessary or else your hamburger icon wont show up 
    actionBarDrawerToggle.syncState(); 
} 

希望這有助於設置導航抽屜。

+0

它幫助,謝謝,欣賞它:) – Anonymous

0
<android.support.v4.widget.DrawerLayout android:background="@drawable/icon" android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/drawerLayout" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"> 

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

<ListView android:background="#ff8800" android:layout_height="match_parent" android:layout_width="240dp" android:id="@+id/lvLeft" android:dividerHeight="0dp" android:divider="@android:color/holo_blue_bright" android:choiceMode="singleChoice" android:layout_gravity="left"> </ListView> 

<ListView android:background="#0077ff" android:layout_height="match_parent" android:layout_width="240dp" android:id="@+id/lvRight" android:dividerHeight="0dp" android:divider="@android:color/transparent" android:choiceMode="singleChoice" android:layout_gravity="right"> </ListView> 

</android.support.v4.widget.DrawerLayout> 
+0

除了格式化的代碼外,您還應該給解答添加解釋 –