抽屉视图没有显示

时间:2016-09-06 09:49:03

标签: android android-navigation-drawer

我的抽屉视图没有显示它在我没有对布局进行任何更改之前工作正常或MainActivity现在当我检查它什么都没有显示时

我的XML

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#71b6ca"
    android:id="@+id/toolbar"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:title="DocVids" />

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

<FrameLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/containerView">
 </FrameLayout>



<android.support.design.widget.NavigationView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:layout_marginTop="-24dp"
    android:id="@+id/mynav"
    />
</android.support.v4.widget.DrawerLayout>

</LinearLayout>

抽屉菜单

  <item android:title="FeedBack"
      android:id="@+id/nav_item_feedback"
      android:icon="@drawable/feedback_icon"/>

<item android:title="Rate Us"
      android:id="@+id/nav_item_draft"
      android:icon="@drawable/rateus_icon"/>



<item android:title="Others">
    <menu>
        <item
            android:title="About"
            android:icon="@drawable/about_icon"/>
        <item android:title="Help"
            android:icon="@drawable/help"/>
    </menu>
</item>

MainActivity

public class MainActivity extends AppCompatActivity{
DrawerLayout mDrawerLayout;
NavigationView mNavigationView;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    setTitle("DocVids");
    /**
     *Setup the DrawerLayout and NavigationView
     */

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    mDrawerLayout.bringToFront();
    mNavigationView = (NavigationView) findViewById(R.id.mynav) ;

    /**
     * Lets inflate the very first fragment
     * Here , we are inflating the TabFragment as the first Fragment
     */

    mFragmentManager = getSupportFragmentManager();
    mFragmentTransaction = mFragmentManager.beginTransaction();
    mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
    /**
     * Setup click events on the Navigation View Items.
     */

    mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            mDrawerLayout.closeDrawers();



            if (menuItem.getItemId() == R.id.nav_item_feedback) {
                FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.containerView,new FeedBack()).commit();

            }

            if (menuItem.getItemId() == R.id.nav_item_fav) {
                FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
                xfragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
            }

            return false;
        }

    });


    /**
     * Setup Drawer Toggle of the Toolbar
     */

    android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
    ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, toolbar,R.string.app_name,
            R.string.app_name);

    mDrawerToggle.syncState();
    //mDrawerLayout.setDrawerListener(mDrawerToggle);
    mDrawerLayout.addDrawerListener(mDrawerToggle);

}

2 个答案:

答案 0 :(得分:2)

我的xml 中 为导航抽屉制作xml,如下所示: 将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">

<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_home"
    app:menu="@menu/activity_home_drawer" />

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

答案 1 :(得分:0)

您必须将抽屉布局作为父元素,因此您的布局将变为

/admin/people/permissions

检查this示例以获取有关导航菜单用法的更多信息