[英]Navigation drawer does not open when pressing the hamburger icon in toolbar
I am trying to toggle the navigation view by the hamburger icon. 我正在尝试通过汉堡包图标切换导航视图。 The navigation view is appearing on sliding from the left. 导航视图从左侧滑动出现。 The following is the code that I have included in my MainActivity "onCreate Function" 以下是我包含在MainActivity“ onCreate Function”中的代码
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setVisibility(View.VISIBLE);
setSupportActionBar(toolbar);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, mDrawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerLayout.addDrawerListener(toggle);
getSupportActionBar().setHomeButtonEnabled(true);
toggle.syncState();
I also have tried this code block which doesn't seem to work. 我也试过了这个似乎无效的代码块。
@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
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
I also tried this 我也尝试过
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
mDrawerLayout = findViewById(R.id.drawer_layout);
mDrawerLayout.openDrawer(GravityCompat.START); // OPEN DRAWER
return true;
}
return super.onOptionsItemSelected(item);
}
My main_activity.xml is this 我的main_activity.xml是这个
<?xml version="1.0" encoding="utf-8"?>
<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">
>
<RelativeLayout
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/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/app_bar_main"
android:layout_height="match_parent"
android:layout_width="match_parent" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="@color/colorPrimary"
app:itemIconTint="@drawable/bottom_navigation_bar"
app:itemTextColor="@drawable/bottom_navigation_bar"
app:menu="@menu/my_navigation_items" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottom_navigation"
android:layout_alignParentTop="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</RelativeLayout>
<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_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
This is my app_bar_main.xml which is where I define the ui of toolbar. 这是我的app_bar_main.xml,它是我定义工具栏的ui的地方。
enter code here
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.android.moodindigo.MainActivity">
<!--<RelativeLayout-->
<!--android:id="@+id/relativelayout_for_fragment"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--></RelativeLayout>-->
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:title="Mood Indigo"
/>
</android.support.design.widget.AppBarLayout>
<!--<include layout="@layout/content_main" />-->
</android.support.design.widget.CoordinatorLayout>
Any help would be highly appreciated. 任何帮助将不胜感激。 I don't know what I am doing wrong. 我不知道我在做什么错。 Please let me know if anything else is needed. 请让我知道是否还有其他需要。 I didn't paste the whole code here as the project is quite big. 由于项目很大,因此我没有在此处粘贴整个代码。 I checked many StackOverflow post for this and I tried to do the same thing but it doesn't seem to work. 我为此检查了很多StackOverflow帖子,并尝试做同样的事情,但似乎不起作用。
try this in the onCreate of your main activity : 在您的主要活动的onCreate中尝试以下操作:
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
i hope it will help you! 希望对您有帮助!
In your ActionBarDrawerToggle constructor you use toolbar as third parameter. 在ActionBarDrawerToggle构造函数中,将工具栏用作第三个参数。 I am not sure if it is valid, since the official documentation for ActionBarDrawerToggle constructor parameters quotes: 我不确定它是否有效,因为ActionBarDrawerToggle构造函数参数的官方文档引用了:
Parameters: 参数:
activity - Activity: the Activity hosting the drawer 活动-活动:托管抽屉的活动
drawerLayout - Drawerlayout: the DrawerLayout to link to the given Activity's ActionBar 抽屉式布局-Drawerlayout:链接到给定Activity的ActionBar的DrawerLayout
drawerImageRes - int: A Drawable resource to use as the drawer indicator cabinetImageRes-int:可用作抽屉指示器的Drawable资源
openDrawerContentDescRes - int: A String resource to describe the "open drawer" action for accessibility openDrawerContentDescRes-int:一个字符串资源,用于描述可访问性的“打开抽屉”操作
closeDrawerContentDescRes - int: A String resource to describe the "close drawer" action for accessibility closeDrawerContentDescRes-int:一个字符串资源,用于描述可访问性的“关闭抽屉”操作
So, the third parameter should be Drawable Resource but toolbar is a View. 因此,第三个参数应该是Drawable Resource,而工具栏是View。
Edit: As you are using v7 and not v4, there is one sentence from official documentation https://developer.android.com/reference/android/support/v7/app/ActionBarDrawerToggle.html I think is important: 编辑:当您使用的是v7而不是v4时,官方文档中有一句话https://developer.android.com/reference/android/support/v7/app/ActionBarDrawerToggle.html我认为很重要:
Please use ActionBarDrawerToggle(Activity, DrawerLayout, int, int) if you are setting the Toolbar as the ActionBar of your activity. 如果将工具栏设置为活动的ActionBar,请使用ActionBarDrawerToggle(Activity,DrawerLayout,int,int)。
So it seems you should use differnt constructor from the one you are using now. 因此,似乎您应该使用与现在使用的构造函数不同的构造函数。
Hope this will help. 希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.