2017-04-26 118 views
1

我有我的抽屜式導航欄,我需要打開它,但我不能>>導航抽屜沒有開始

這裏是我的drawer_layout.xml:

<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<!-- This LinearLayout represents the contents of the screen --> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <!-- The ActionBar displayed at the top --> 
    <include 
     layout="@layout/activity_display" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <!-- The main content view where fragments are loaded --> 
    <FrameLayout 
     android:id="@+id/flContent" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</LinearLayout> 

<!-- The navigation drawer that comes from the left --> 
<!-- Note that `android:layout_gravity` needs to be set to 'start' --> 
<android.support.design.widget.NavigationView 
    android:id="@+id/nvView" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="right" 
    android:background="@android:color/white" 
    app:menu="@menu/drawer_view" /> 

這是我的工具欄在activity_display.xml:

<android.support.v7.widget.Toolbar 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:id="@+id/myToolBar" 
    android:background="@color/top_bar_backgroung"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/dots_vertical" 
     android:textSize="40sp" 
     android:layout_marginRight="10dp" 
     android:layout_gravity="center" 
     android:gravity="center" 
     android:id="@+id/openDrawer" 
     /> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="60dp" 
     android:src="@drawable/logo" 
     android:scaleType="centerInside"/> 
</android.support.v7.widget.Toolbar> 

,這裏是我怎麼打開我的抽屜裏:

private DrawerLayout mDrawer; 
private Toolbar toolbar; 
private NavigationView nvDrawer; 

toolbar = (Toolbar) findViewById(R.id.myToolBar); 
    setSupportActionBar(toolbar); 



    // Find our drawer view 
    View viewDrawer = getLayoutInflater().inflate(R.layout.drawer_layout, null); 
    mDrawer = (DrawerLayout) viewDrawer.findViewById(R.id.drawer_layout); 
    nvDrawer = (NavigationView) viewDrawer.findViewById(R.id.nvView); 
    setupDrawerContent(nvDrawer); 
    //gridView = (GridView) findViewById(R.id.displayGridView); 



    drawer.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Log.i(TAG, "onClick: "); 
      mDrawer.openDrawer(GravityCompat.END); 
     } 
    }); 

private void setupDrawerContent(NavigationView navigationView) { 
    navigationView.setNavigationItemSelectedListener(
      new NavigationView.OnNavigationItemSelectedListener() { 
       @Override 
       public boolean onNavigationItemSelected(MenuItem menuItem) { 
        selectDrawerItem(menuItem); 
        return true; 
       } 
      }); 
} 

public void selectDrawerItem(MenuItem menuItem) { 
    // Create a new fragment and specify the fragment to show based on nav item clicked 
    android.support.v4.app.Fragment fragment1 = null; 
    Class fragmentClass = null; 
    switch(menuItem.getItemId()) { 
     case R.id.nav_home_fragment: 
      Toast.makeText(this, "home", Toast.LENGTH_SHORT).show(); 
      //fragmentClass = FirstFragment.class; 
      break; 
     case R.id.nav_personal_fragment: 
      //fragmentClass = SecondFragment.class; 
      break; 
     case R.id.nav_avilableCareer_fragment: 
      //fragmentClass = ThirdFragment.class; 
      break; 
     case R.id.nav_declareCareer_fragment: 
      //fragmentClass = FourdFragment.class; 
      break; 
     default: 
      //fragmentClass = FirstFragment.class; 
    } 

    try { 
     fragment = (Fragment) fragmentClass.newInstance(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    // Insert the fragment by replacing any existing fragment 
    FragmentManager fragmentManager = getSupportFragmentManager(); 
    fragmentManager.beginTransaction().replace(R.id.flContent, fragment1).commit(); 

    // Highlight the selected item has been done by NavigationView 
    menuItem.setChecked(true); 
    // Set action bar title 
    setTitle(menuItem.getTitle()); 
    // Close the navigation drawer 
    mDrawer.closeDrawers(); 
} 

我不知道爲什麼我不能啓動我的導航平局.. 我看不到它在屏幕上。

回答

0

寫入結束,而不是在正確的XML文件

android:layout_gravity="right" 

android:layout_gravity="end" 

或替代的解決方案

使用

mDrawer.openDrawer(GravityCompat.RIGHT); 

代替

mDrawer.openDrawer(GravityCompat.END); 
+0

它是這樣的之前我改變它,但它不工作 –