导航抽屉未启动

时间:2017-04-26 11:46:07

标签: android android-navigation-drawer

我有我的导航抽屉,我需要打开它,但我不能>>

这是我的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();
}

我不知道为什么我无法启动我的导航画.. 我无法在屏幕上看到它。

1 个答案:

答案 0 :(得分:0)

在xml文件中写入end而不是right

  android:layout_gravity="right"

 android:layout_gravity="end"

或替代解决方案

使用

mDrawer.openDrawer(GravityCompat.RIGHT);

而不是

mDrawer.openDrawer(GravityCompat.END);