单击工具栏上的汉堡包图标不会打开导航抽屉
Clicking hamburger icon on Toolbar does not open Navigation Drawer
我有一个简单的 android.support.v7.widget.Toolbar
,我想做的就是通过按左上角的 "hamburger" 图标打开 NavigationDrawer。 "hamburger" 按钮可见,当我开始从左侧拉动时,我看到按钮上有动画,但按下按钮并没有像我预期的那样 open/close NavigationDrawer。我已经按照 [Google 文档][1] 进行操作,但仍然无法解决这个问题。抱歉造成任何混淆,下面是我目前尝试使用的简化代码:
public class MainActivity extends AppCompatActivity implements
View.OnClickListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("NICK", "button button button..................");
}
});
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
NavigationView n = (NavigationView) findViewById(R.id.nav);
mDrawerLayout.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Log.d("NICK", "button button button..................");
}
});
//mDrawerLayout.setDrawerListener(mDrawerToggle);
n.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
////.......
}
mDrawerLayout.closeDrawers(); // CLOSE DRAWER
return true;
}
});
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d("NICK","CWECNEWKVNERIPNVIEWNFVIPEWNVIPEWNVPIEWNVPIEWNVPIEWNVPIRWNVPRWVPO");
switch (item.getItemId()) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START); // OPEN DRAWER
Log.d("NICK","CWECNEWKVNERIPNVIEWNFVIPEWNVIPEWNVPIEWNVPIEWNVPIEWNVPIRWNVPRWVPO");
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.drawer, menu);
return true;
}
}
}
事实上,当 运行 时我没有得到任何日志调试语句。
这基本上就是我遇到的问题:。我已经关注了这个,但它不起作用。
据我了解,按下汉堡包图标时会调用 setNavigationOnClickListener
,这就是我集中精力正确处理事件的地方,因为当我按下按钮时,我没有得到我的日志声明。如果这个想法不正确,请告诉我。 https://developer.android.com/reference/android/widget/Toolbar.html#setNavigationOnClickListener(android.view.View.OnClickListener)
我的布局:
ActivityMain.xml
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/d"
android:background="@drawable/home_wall">
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:layout_marginBottom="10dp"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_marginTop="25dp"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" />
<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_width="match_parent"
android:id="@+id/drawer"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<ImageView
android:layout_width="fill_parent"
android:layout_height="200dp"
android:id="@+id/imageView"
android:src="@drawable/trans2"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:paddingBottom="300dp" />
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/d8"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/imageView"
android:layout_alignStart="@+id/imageView"
android:paddingTop="0dp">
<Button
android:layout_width="75dp"
android:layout_height="50dp"
android:text="Gallery"
android:id="@+id/save_button"
android:background="#dd2c00" android:textColor="#fff"
android:layout_below="@+id/Purchases"
android:layout_toRightOf="@+id/start_button"
android:layout_toEndOf="@+id/start_button" />
<Button
android:layout_width="125dp"
android:layout_height="50dp"
android:text="Store"
android:id="@+id/Purchases"
android:background="#ff6e40" android:textColor="#fff"
android:layout_above="@+id/instructions_button6"
android:layout_toLeftOf="@+id/start_button"
android:layout_toStartOf="@+id/start_button"
android:layout_marginBottom="98dp" />
<Button
android:layout_width="75dp"
android:layout_height="50dp"
android:text="Help"
android:id="@+id/instructions_button6"
android:background="#dd2c00" android:textColor="#fff"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/start_button"
android:layout_toStartOf="@+id/start_button"
android:layout_marginLeft="5dp"
android:layout_marginBottom="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="300dp"
android:text="Start"
android:id="@+id/start_button"
android:background="#ff3d00"
android:textColor="#fff"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp" />
<Button
android:layout_width="125dp"
android:layout_height="50dp"
android:text="Achievements"
android:id="@+id/Scores"
android:background="#ff6e40" android:textColor="#fff"
android:layout_alignTop="@+id/Purchases"
android:layout_toRightOf="@+id/start_button"
android:layout_toEndOf="@+id/start_button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to the quiz!"
android:id="@+id/textView"
android:textColor="#fff"
android:textSize="20dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp" />
<!-- sign-in button -->
<com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_button"
android:layout_width="110dp"
android:layout_height="50dp"
android:layout_above="@+id/start_button"
android:layout_centerHorizontal="true"
android:visibility="visible" />
<!-- sign-out button -->
<Button
android:id="@+id/sign_out_button"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:text="Sign Out"
android:visibility="invisible"
android:background="#dd4b39"
android:textColor="#fff"
android:layout_alignTop="@+id/sign_in_button"
android:layout_centerHorizontal="true"
android:layout_marginTop="160dp" />
</RelativeLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff"
android:id="@+id/nav"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/drawer"/>
</android.support.v4.widget.DrawerLayout>
Drawer.xml
:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_menu"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="Google Play Games"
android:icon="@drawable/ic_local_airport_white_48dp">
<menu>
<item
android:id="@+id/Sign_in_drawer"
android:icon="@drawable/games_controller_grey"
android:title="Sign in" />
<item
android:id="@+id/ach"
android:icon="@drawable/games_achievements"
android:title="Achievements" />
</menu>
</item>
<item android:title="Start a Quiz"
android:icon="@drawable/ic_local_airport_white_48dp">
<menu>
<item
android:id="@+id/quizStart25"
android:icon="@drawable/ic_local_airport_white_48dp"
android:title="25 Questions" />
<item
android:id="@+id/quizStart10"
android:icon="@drawable/ic_local_airport_white_48dp"
android:title="10 Questions" />
</menu>
</item>
<group
android:checkableBehavior="single">
<item
android:id="@+id/gallery"
android:icon="@drawable/ic_photo_library_white_48dp"
android:title="Gallery" />
<item
android:id="@+id/stats"
android:icon="@drawable/ic_toc_white_48dp"
android:title="Statistics" />
<item
android:id="@+id/store"
android:icon="@drawable/ic_shop_white_48dp"
android:title="Store" />
<item
android:id="@+id/settings"
android:icon="@drawable/ic_settings_white_48dp"
android:title="Settings" />
<item
android:id="@+id/about"
android:icon="@drawable/ic_info_white_48dp"
android:title="About" />
</group>
<item android:title="Support">
<menu>
<item
android:id="@+id/help_drawer"
android:icon="@drawable/ic_help_white_48dp"
android:title="Help" />
<item
android:id="@+id/report"
android:icon="@drawable/ic_report_problem_white_48dp"
android:title="Contact Developer" />
<item
android:id="@+id/GPlusCommunity"
android:icon="@drawable/btn_g_white_normal"
android:title="Google+ Community" />
</menu>
</item>
您需要同步抽屉开关:
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
编辑:该代码对我有用(从您的 post 复制)
public class TempActivity extends AppCompatActivity {
private ActionBarDrawerToggle mDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.temp);
setupDrawer();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
private void setupDrawer() {
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.my_drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,toolbar,R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
}
<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/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<RelativeLayout
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:text="DRAMER MENU" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
但是如果你使用的是新的NavigationView
,那么你不需要切换等。Here is a good example如何使用它。
对于抽屉开关,您需要将 "display home as up" 设置为 false:
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
在您的 ActivityMain.xml 中,工具栏位于 DrawerLayout 之外。那就是问题所在。如果您希望 Toolbar 与 DrawLayout 交互,Toolbar 需要是 DrawerLayout 的子项。
要解决此问题,请将 DrawerLayout 设为 activity 的根目录。这是 documentation。相关报价是:
To add a navigation drawer, declare your user interface with a
DrawerLayout object as the root view of your layout. Inside the
DrawerLayout, add one view that contains the main content for the
screen (your primary layout when the drawer is hidden) and another
view that contains the contents of the navigation drawer.
所以基本上,将您的 ActivityMain.xml 构造成这样:
<android.support.v4.widget.DrawerLayout ...>
<RelativeLayout ...>
<android.support.v7.widget.Toolbar .../>
<!-- Your other content goes here -->
</RelativeLayout>
<android.support.design.widget.NavigationView .../>
</android.support.v4.widget.DrawerLayout>
这应该可以解决问题。
由于我对你的问题没有具体的答案,我想建议一个完全不同的方法:
在我的项目中,我使用的是 Mike Penz 开发的 Material Drawer。
它看起来真的很不错,很容易实现,你不用担心太多。
所以,如果您找不到解决问题的方法,可以试一试。
重写 onOptionsItemSelected
方法并使用下面的方法
if(item.getItemId() == android.R.id.home){ // use android.R.id
mDrawerLayout.openDrawer(Gravity.LEFT);
}
使用ActionBarDrawerToggle
并实施public boolean onOptionsItemSelected(MenuItem item)
工具栏不必位于 DrawerLayout 中,这不太可能是导致此问题的原因。 toggle.onOptionsItemSelected(item)
查找所选项目的 ID,如果它是 android.R.id.home
,则抽屉的可见性被切换。因此,工具栏或创建主菜单项的任何视图都可以放置在布局中的任何位置。
在您的 activity 内:
ActionBarDrawerToggle toggle;
private void setupDrawerToggleInActionBar() {
// assuming a Toolbar has been initialized in your onCreate
this.setSupportActionBar(toolbar);
// setup the action bar properties that give us a hamburger menu
ActionBar actionBar = this.getSupportActionBar();
if(actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
}
// the toggle allows for the simplest of open/close handling
toggle = new ActionBarDrawerToggle(this,
drawerLayout,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
// drawerListener must be set before syncState is called
drawerLayout.setDrawerListener(toggle);
toggle.setDrawerIndicatorEnabled(true);
toggle.syncState();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// This is required to make the drawer toggle work
if(toggle.onOptionsItemSelected(item)) {
return true;
}
/*
* if you have other menu items in your activity/toolbar
* handle them here and return true
*/
return super.onOptionsItemSelected(item);
}
只需在 activity 选项菜单管理函数上调用 ActionBarDrawerToggle class 的 onOptionsItemSelected(MenuItem menuItem) 函数,如下所示。
mDrawerToggle= new ActionBarDrawerToggle(activity, drawerLayout, R.string.nav_drawer_accessbility_drawer_open,
R.string.nav_drawer_accessbility_drawer_close);
@Override
public boolean onOptionsItemSelected(final android.view.MenuItem item) {
navigation().getOptionsMenuInflater(this).closeSearchView();
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this behavior.
mDrawerToggle.onOptionsItemSelected(item);
return super.onOptionsItemSelected(item);
}
或
重写 activity 的 onOptionsItemSelected,如下所示
public boolean onOptionsItemSelected(MenuItem item) {
if (item != null && item.getItemId() == android.R.id.home) {
toggle();
}
return super.onOptionsItemSelected(item);
}
private void toggle() {
if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) {
mDrawerLayout.closeDrawer(GravityCompat.START);
} else {
mDrawerLayout.openDrawer(GravityCompat.START);
}
}
虽然这里接受的答案没有问题,但正如他们所说 "Prevention is better than cure".. 添加 mDrawerToggle.syncState();在 onPostCreate 和 onConfigurationChanged() 中效果更好,正如上面@mbmc 所回答的那样:
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
如果它对某人有帮助,对我来说它发生了同样的事情,因为两次调用 setSupportActionBar(toolbar)
的愚蠢错误。只需在 onCreate 方法中调用一次,再也不会调用它。我的错误是我后来用另一种方法调用了它。
在遇到很多不同的问题后,我找到了一种方法,可以将 Toolbar Widget 放在 AppBarLayout 中。
确保小部件工具栏顶部没有其他布局!
<androidx.drawerlayout.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/drawable_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
xmlns:android="http://schemas.android.com/apk/res/android">**
<androidx.appcompat.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_page_toolbar"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>**
.... YOUR LAYOUT ...
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginBottom="3dp"
app:menu="@menu/navigation_menu">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
不知道为什么...但是将 return 值更改为 false 而不是 true 为我修复了火腿图标。 onOptionItemSelected 覆盖方法
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if(item.getItemId() == R.id.logout){
ParseUser.logOut();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
Toast.makeText(this, "Logout Successful!", Toast.LENGTH_SHORT).show();
} else if(item.getItemId() == R.id.action_settings){
Toast.makeText(this, "No setting to update! Will be available later...", Toast.LENGTH_SHORT).show();
}
return true; // here change this to false
}
我通过将 drawerLayout 的 openable 赋予 navigateUp 函数来修复它。
更多信息navigateUp Doc
override fun onSupportNavigateUp(): Boolean {
val navController = this.findNavController(R.id.host_fragment)
return NavigationUI.navigateUp(navController,drawer_layout)
}
如果上述答案none有效,请再次核对,
首先你需要调用setSupportActionBar(toolbar);然后为导航抽屉做必要的工作。由于这个愚蠢的错误,我浪费了我的 2 个小时。
我遇到了同样的问题。但是,它并没有发生在所有片段上。它发生在一个特定的片段上。
首先,请按照@hungryghost 上面提到的回答。然后执行以下操作。
添加以下代码有助于在单击汉堡包图标时打开抽屉。它对我来说完美无缺。非常感谢你。 :) 如果这不是正确的方式,我深表歉意。
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int itemId = item.getItemId();
if(itemId == android.R.id.home){
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
我发现这个问题的一个简单解决方案是在导航抽屉功能之前调用 setSupportActionBar(toolbar) 方法。
我有一个简单的 android.support.v7.widget.Toolbar
,我想做的就是通过按左上角的 "hamburger" 图标打开 NavigationDrawer。 "hamburger" 按钮可见,当我开始从左侧拉动时,我看到按钮上有动画,但按下按钮并没有像我预期的那样 open/close NavigationDrawer。我已经按照 [Google 文档][1] 进行操作,但仍然无法解决这个问题。抱歉造成任何混淆,下面是我目前尝试使用的简化代码:
public class MainActivity extends AppCompatActivity implements
View.OnClickListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("NICK", "button button button..................");
}
});
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
NavigationView n = (NavigationView) findViewById(R.id.nav);
mDrawerLayout.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Log.d("NICK", "button button button..................");
}
});
//mDrawerLayout.setDrawerListener(mDrawerToggle);
n.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
////.......
}
mDrawerLayout.closeDrawers(); // CLOSE DRAWER
return true;
}
});
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d("NICK","CWECNEWKVNERIPNVIEWNFVIPEWNVIPEWNVPIEWNVPIEWNVPIEWNVPIRWNVPRWVPO");
switch (item.getItemId()) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START); // OPEN DRAWER
Log.d("NICK","CWECNEWKVNERIPNVIEWNFVIPEWNVIPEWNVPIEWNVPIEWNVPIEWNVPIRWNVPRWVPO");
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.drawer, menu);
return true;
}
}
}
事实上,当 运行 时我没有得到任何日志调试语句。
这基本上就是我遇到的问题:。我已经关注了这个,但它不起作用。
据我了解,按下汉堡包图标时会调用 setNavigationOnClickListener
,这就是我集中精力正确处理事件的地方,因为当我按下按钮时,我没有得到我的日志声明。如果这个想法不正确,请告诉我。 https://developer.android.com/reference/android/widget/Toolbar.html#setNavigationOnClickListener(android.view.View.OnClickListener)
我的布局:
ActivityMain.xml
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/d"
android:background="@drawable/home_wall">
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:layout_marginBottom="10dp"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_marginTop="25dp"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" />
<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_width="match_parent"
android:id="@+id/drawer"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<ImageView
android:layout_width="fill_parent"
android:layout_height="200dp"
android:id="@+id/imageView"
android:src="@drawable/trans2"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:paddingBottom="300dp" />
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/d8"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/imageView"
android:layout_alignStart="@+id/imageView"
android:paddingTop="0dp">
<Button
android:layout_width="75dp"
android:layout_height="50dp"
android:text="Gallery"
android:id="@+id/save_button"
android:background="#dd2c00" android:textColor="#fff"
android:layout_below="@+id/Purchases"
android:layout_toRightOf="@+id/start_button"
android:layout_toEndOf="@+id/start_button" />
<Button
android:layout_width="125dp"
android:layout_height="50dp"
android:text="Store"
android:id="@+id/Purchases"
android:background="#ff6e40" android:textColor="#fff"
android:layout_above="@+id/instructions_button6"
android:layout_toLeftOf="@+id/start_button"
android:layout_toStartOf="@+id/start_button"
android:layout_marginBottom="98dp" />
<Button
android:layout_width="75dp"
android:layout_height="50dp"
android:text="Help"
android:id="@+id/instructions_button6"
android:background="#dd2c00" android:textColor="#fff"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/start_button"
android:layout_toStartOf="@+id/start_button"
android:layout_marginLeft="5dp"
android:layout_marginBottom="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="300dp"
android:text="Start"
android:id="@+id/start_button"
android:background="#ff3d00"
android:textColor="#fff"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp" />
<Button
android:layout_width="125dp"
android:layout_height="50dp"
android:text="Achievements"
android:id="@+id/Scores"
android:background="#ff6e40" android:textColor="#fff"
android:layout_alignTop="@+id/Purchases"
android:layout_toRightOf="@+id/start_button"
android:layout_toEndOf="@+id/start_button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to the quiz!"
android:id="@+id/textView"
android:textColor="#fff"
android:textSize="20dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp" />
<!-- sign-in button -->
<com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_button"
android:layout_width="110dp"
android:layout_height="50dp"
android:layout_above="@+id/start_button"
android:layout_centerHorizontal="true"
android:visibility="visible" />
<!-- sign-out button -->
<Button
android:id="@+id/sign_out_button"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:text="Sign Out"
android:visibility="invisible"
android:background="#dd4b39"
android:textColor="#fff"
android:layout_alignTop="@+id/sign_in_button"
android:layout_centerHorizontal="true"
android:layout_marginTop="160dp" />
</RelativeLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff"
android:id="@+id/nav"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/drawer"/>
</android.support.v4.widget.DrawerLayout>
Drawer.xml
:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_menu"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="Google Play Games"
android:icon="@drawable/ic_local_airport_white_48dp">
<menu>
<item
android:id="@+id/Sign_in_drawer"
android:icon="@drawable/games_controller_grey"
android:title="Sign in" />
<item
android:id="@+id/ach"
android:icon="@drawable/games_achievements"
android:title="Achievements" />
</menu>
</item>
<item android:title="Start a Quiz"
android:icon="@drawable/ic_local_airport_white_48dp">
<menu>
<item
android:id="@+id/quizStart25"
android:icon="@drawable/ic_local_airport_white_48dp"
android:title="25 Questions" />
<item
android:id="@+id/quizStart10"
android:icon="@drawable/ic_local_airport_white_48dp"
android:title="10 Questions" />
</menu>
</item>
<group
android:checkableBehavior="single">
<item
android:id="@+id/gallery"
android:icon="@drawable/ic_photo_library_white_48dp"
android:title="Gallery" />
<item
android:id="@+id/stats"
android:icon="@drawable/ic_toc_white_48dp"
android:title="Statistics" />
<item
android:id="@+id/store"
android:icon="@drawable/ic_shop_white_48dp"
android:title="Store" />
<item
android:id="@+id/settings"
android:icon="@drawable/ic_settings_white_48dp"
android:title="Settings" />
<item
android:id="@+id/about"
android:icon="@drawable/ic_info_white_48dp"
android:title="About" />
</group>
<item android:title="Support">
<menu>
<item
android:id="@+id/help_drawer"
android:icon="@drawable/ic_help_white_48dp"
android:title="Help" />
<item
android:id="@+id/report"
android:icon="@drawable/ic_report_problem_white_48dp"
android:title="Contact Developer" />
<item
android:id="@+id/GPlusCommunity"
android:icon="@drawable/btn_g_white_normal"
android:title="Google+ Community" />
</menu>
</item>
您需要同步抽屉开关:
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
编辑:该代码对我有用(从您的 post 复制)
public class TempActivity extends AppCompatActivity {
private ActionBarDrawerToggle mDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.temp);
setupDrawer();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
private void setupDrawer() {
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.my_drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,toolbar,R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
}
<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/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<RelativeLayout
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:text="DRAMER MENU" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
但是如果你使用的是新的NavigationView
,那么你不需要切换等。Here is a good example如何使用它。
对于抽屉开关,您需要将 "display home as up" 设置为 false:
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
在您的 ActivityMain.xml 中,工具栏位于 DrawerLayout 之外。那就是问题所在。如果您希望 Toolbar 与 DrawLayout 交互,Toolbar 需要是 DrawerLayout 的子项。
要解决此问题,请将 DrawerLayout 设为 activity 的根目录。这是 documentation。相关报价是:
To add a navigation drawer, declare your user interface with a DrawerLayout object as the root view of your layout. Inside the DrawerLayout, add one view that contains the main content for the screen (your primary layout when the drawer is hidden) and another view that contains the contents of the navigation drawer.
所以基本上,将您的 ActivityMain.xml 构造成这样:
<android.support.v4.widget.DrawerLayout ...>
<RelativeLayout ...>
<android.support.v7.widget.Toolbar .../>
<!-- Your other content goes here -->
</RelativeLayout>
<android.support.design.widget.NavigationView .../>
</android.support.v4.widget.DrawerLayout>
这应该可以解决问题。
由于我对你的问题没有具体的答案,我想建议一个完全不同的方法:
在我的项目中,我使用的是 Mike Penz 开发的 Material Drawer。 它看起来真的很不错,很容易实现,你不用担心太多。
所以,如果您找不到解决问题的方法,可以试一试。
重写 onOptionsItemSelected
方法并使用下面的方法
if(item.getItemId() == android.R.id.home){ // use android.R.id
mDrawerLayout.openDrawer(Gravity.LEFT);
}
使用ActionBarDrawerToggle
并实施public boolean onOptionsItemSelected(MenuItem item)
工具栏不必位于 DrawerLayout 中,这不太可能是导致此问题的原因。 toggle.onOptionsItemSelected(item)
查找所选项目的 ID,如果它是 android.R.id.home
,则抽屉的可见性被切换。因此,工具栏或创建主菜单项的任何视图都可以放置在布局中的任何位置。
在您的 activity 内:
ActionBarDrawerToggle toggle;
private void setupDrawerToggleInActionBar() {
// assuming a Toolbar has been initialized in your onCreate
this.setSupportActionBar(toolbar);
// setup the action bar properties that give us a hamburger menu
ActionBar actionBar = this.getSupportActionBar();
if(actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
}
// the toggle allows for the simplest of open/close handling
toggle = new ActionBarDrawerToggle(this,
drawerLayout,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
// drawerListener must be set before syncState is called
drawerLayout.setDrawerListener(toggle);
toggle.setDrawerIndicatorEnabled(true);
toggle.syncState();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// This is required to make the drawer toggle work
if(toggle.onOptionsItemSelected(item)) {
return true;
}
/*
* if you have other menu items in your activity/toolbar
* handle them here and return true
*/
return super.onOptionsItemSelected(item);
}
只需在 activity 选项菜单管理函数上调用 ActionBarDrawerToggle class 的 onOptionsItemSelected(MenuItem menuItem) 函数,如下所示。
mDrawerToggle= new ActionBarDrawerToggle(activity, drawerLayout, R.string.nav_drawer_accessbility_drawer_open,
R.string.nav_drawer_accessbility_drawer_close);
@Override
public boolean onOptionsItemSelected(final android.view.MenuItem item) {
navigation().getOptionsMenuInflater(this).closeSearchView();
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this behavior.
mDrawerToggle.onOptionsItemSelected(item);
return super.onOptionsItemSelected(item);
}
或
重写 activity 的 onOptionsItemSelected,如下所示
public boolean onOptionsItemSelected(MenuItem item) {
if (item != null && item.getItemId() == android.R.id.home) {
toggle();
}
return super.onOptionsItemSelected(item);
}
private void toggle() {
if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) {
mDrawerLayout.closeDrawer(GravityCompat.START);
} else {
mDrawerLayout.openDrawer(GravityCompat.START);
}
}
虽然这里接受的答案没有问题,但正如他们所说 "Prevention is better than cure".. 添加 mDrawerToggle.syncState();在 onPostCreate 和 onConfigurationChanged() 中效果更好,正如上面@mbmc 所回答的那样:
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
如果它对某人有帮助,对我来说它发生了同样的事情,因为两次调用 setSupportActionBar(toolbar)
的愚蠢错误。只需在 onCreate 方法中调用一次,再也不会调用它。我的错误是我后来用另一种方法调用了它。
在遇到很多不同的问题后,我找到了一种方法,可以将 Toolbar Widget 放在 AppBarLayout 中。
确保小部件工具栏顶部没有其他布局!
<androidx.drawerlayout.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/drawable_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
xmlns:android="http://schemas.android.com/apk/res/android">**
<androidx.appcompat.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_page_toolbar"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>**
.... YOUR LAYOUT ...
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginBottom="3dp"
app:menu="@menu/navigation_menu">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
不知道为什么...但是将 return 值更改为 false 而不是 true 为我修复了火腿图标。 onOptionItemSelected 覆盖方法
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if(item.getItemId() == R.id.logout){
ParseUser.logOut();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
Toast.makeText(this, "Logout Successful!", Toast.LENGTH_SHORT).show();
} else if(item.getItemId() == R.id.action_settings){
Toast.makeText(this, "No setting to update! Will be available later...", Toast.LENGTH_SHORT).show();
}
return true; // here change this to false
}
我通过将 drawerLayout 的 openable 赋予 navigateUp 函数来修复它。
更多信息navigateUp Doc
override fun onSupportNavigateUp(): Boolean {
val navController = this.findNavController(R.id.host_fragment)
return NavigationUI.navigateUp(navController,drawer_layout)
}
如果上述答案none有效,请再次核对,
首先你需要调用setSupportActionBar(toolbar);然后为导航抽屉做必要的工作。由于这个愚蠢的错误,我浪费了我的 2 个小时。
我遇到了同样的问题。但是,它并没有发生在所有片段上。它发生在一个特定的片段上。
首先,请按照@hungryghost 上面提到的回答。然后执行以下操作。 添加以下代码有助于在单击汉堡包图标时打开抽屉。它对我来说完美无缺。非常感谢你。 :) 如果这不是正确的方式,我深表歉意。
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int itemId = item.getItemId();
if(itemId == android.R.id.home){
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
我发现这个问题的一个简单解决方案是在导航抽屉功能之前调用 setSupportActionBar(toolbar) 方法。