【问题标题】:How to open Navigation Drawer when click on navigation drawer icon单击导航抽屉图标时如何打开导航抽屉
【发布时间】:2018-08-23 08:18:47
【问题描述】:

点击导航抽屉图标时如何打开导航抽屉。

我想像这样打开导航抽屉

![导航抽屉][1]

我添加了一个菜单,但仍然没有显示

toggle = new ActionBarDrawerToggle(
            DashBordActivity.this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();
    if (navigationView != null) {
        setupDrawerContent(navigationView);
    }


@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement

    return super.onOptionsItemSelected(item);
}

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

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

public void setupToolbar(String title) {
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(title);
    toolbar.setTitleTextColor(Color.BLACK);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

private void setupDrawerContent(NavigationView navigationView) {
    //revision: this don't works, use setOnChildClickListener() and setOnGroupClickListener() above instead
    navigationView.setNavigationItemSelectedListener(
            new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    menuItem.setChecked(true);
                    drawer.closeDrawers();
                    return true;
                }
            });
}

当我点击抽屉时导航抽屉没有打开如何解决这个问题

我的代码中是否缺少任何代码。

【问题讨论】:

  • 请添加您的布局文件和java文件
  • 在项目内添加图标
  • 在你的 onCreate 中,添加这些:Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);setSupportActionBar(toolbar);
  • 将边距顶部(操作栏大小)添加到您的抽屉布局

标签: android navigation-drawer


【解决方案1】:
  Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
       toolbar.setNavigationIcon(R.drawable.menu);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawer.openDrawer(START);
            }
        });

【讨论】:

    【解决方案2】:

    试试这个,

    <item
            android:id="@+id/nav_camera"
            android:icon="@drawable/ic_menu_camera"
            android:title="@string/import" />
    
    </group>
    

    【讨论】:

      【解决方案3】:

      试试这个:

      本教程非常有帮助,例如:-

      • 添加导航抽屉布局
      • 在抽屉布局中设置图标和文本

      我参考这个链接

      https://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

      试试这个很有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-02
        • 2015-09-15
        • 2022-08-15
        • 1970-01-01
        相关资源
        最近更新 更多