android - 抽屉 View 未显示

标签 android navigation-drawer

我的抽屉 View 没有显示它在我没有对布局或 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>

主要 Activity

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);

}

最佳答案

在您的我的 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>

关于android - 抽屉 View 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39345865/

相关文章:

android - 抽屉导航 : unable to show the back icon instead of hamburger icon on the toolbar in the children activities

安卓.view.InflateException : Binary XML file line #1: Error inflating class <unknown>

java.lang.NullPointerException : Attempt to invoke virtual method 'ActionBar.setNavigationMode(int)' on a null object reference 异常

android - ActionBar 菜单项按下背景色

android - 存储动态数据的最佳方式?

java - 使用 gdx 库和 FFT 计算频率 (Java)

Android 选项卡栏 Controller (Fragment 或 Action Bar 或 TabHost)

android - 在通用图像加载器中访问 protected 图像

java - 导航组件 : Passing arguments from global actions results in NullPointerException

flutter - 找不到错误404页面/profileScreen