2017-03-09 57 views
-1

我已經創建了一個應用程序。它工作的很好,但現在我想在主要活動中包含導航抽屜。我知道代碼模板,但爲此我需要創建新的活動。我的問題是將抽屜佈局包含到僅包含Recycler視圖和fab按鈕的現有活動中,如果可能包含?對於大問題抱歉。爲主要活動添加抽屜佈局

+0

你可以擴展你的導航活動到你想使用的活動 – rookieDeveloper

+0

我不能讓你的男人sorry.am完全新到android –

+0

@Arun Basker,你試過我的解決方案嗎? – tahsinRupam

回答

0

你必須把下面的代碼在activity_main.xml中

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
<FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    <android.support.design.widget.FloatingActionButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
    </FrameLayout> 

    <fragment 
     android:id="@+id/fragment_navigation_drawer" 
     android:name="com.example.FragmentDrawer" 
     android:layout_width="@dimen/nav_drawer_width" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     app:layout="@layout/fragment_navigation_drawer" 
     tools:layout="@layout/fragment_navigation_drawer" /> 
</android.support.v4.widget.DrawerLayout> 
+0

我無法將dbactivity擴展到AppCompatActivity。 –

+0

請問您可以發佈您的代碼 – 2017-03-09 06:29:40

2

您可以添加NavigationDrawer一步一步按以下程序:

1)設置你的活動父佈局DrawerLayout

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

2)添加NavigationView在d的底部rawerLayout:

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

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

3)稱爲activity_main_drawer.xml res文件夾下添加菜單文件夾中的XML文件菜單項:

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 

    <group 
     android:id="@+id/top" 
     android:checkableBehavior="single"> 
     <item 
      android:id="@+id/nav_camera" 
      android:icon="@drawable/ic_menu_camera" 
      android:title="Home" /> 
    </group> 

    <group 
     android:id="@+id/middle" 
     android:checkableBehavior="single"> 
     <item 
      android:id="@+id/nav_share" 
      android:icon="@drawable/ic_menu_share" 
      android:title="Directory" /> 
    </group> 

    <group 
     android:id="@+id/bottom" 
     android:checkableBehavior="single"> 
     <item 
      android:id="@+id/nav_send" 
      android:icon="@drawable/ic_menu_send" 
      android:title="About Us" /> 
    </group> 

</menu> 

4)在活動實施NavigationView.OnNavigationItemSelectedListener

public class YourActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { 
} 

5)添加onNavigat ionItemSelected()方法來定義您選擇的導航項目上的操作。

@SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.nav_camera) { 
      // Handle the camera action 
     } 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; 
    } 

6)配置DrawerLayout和活動的設置偵聽到您NavigationView中的onCreate():

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState(); 

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 

7)添加到nav_header_main.xml佈局文件夾中;

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/nav_header_height" 
    android:background="@drawable/side_nav_bar" 
    android:gravity="bottom" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark"> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/nav_header_vertical_spacing" 
     android:src="@android:drawable/sym_def_app_icon" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/nav_header_vertical_spacing" 
     android:text="Android Studio" 
     android:textAppearance="@style/TextAppearance.AppCompat.Body1" /> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="[email protected]" /> 

</LinearLayout> 

如果您要添加的工具欄,下面的代碼粘貼在裏面DrawerLayout頂部:

<android.support.design.widget.CoordinatorLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.design.widget.AppBarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:theme="@style/AppTheme.AppBarOverlay"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="?attr/colorPrimary" 
       app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 

並關閉CoordinatorLayout的NavigationView前右:

</android.support.design.widget.CoordinatorLayout> 

讓我知道怎麼運行的。

+0

那麼nav_header ..sorry我不能發佈我的代碼爲了一些安全目的 –

+1

@ArunBaskar代碼是否工作? – tahsinRupam

+0

嘿工具欄怎麼樣;) –