Android 单击工具栏上的汉堡图标不会打开导航抽屉
我有一个简单的Android 单击工具栏上的汉堡图标不会打开导航抽屉,android,navigation-drawer,android-toolbar,Android,Navigation Drawer,Android Toolbar,我有一个简单的android.support.v7.widget.Toolbar,我要做的就是按下左上角的“汉堡包”图标打开一个导航抽屉。“汉堡包”按钮是可见的,当我开始从左侧拉动时,我会看到按钮上的动画,但按下按钮不会像我预期的那样打开/关闭NavigationDrawer。我已经阅读了[谷歌文档][1],但仍然无法理解这一点。很抱歉造成混淆,以下是我当前尝试使用的简化代码: public class MainActivity extends AppCompatActivity impleme
android.support.v7.widget.Toolbar
,我要做的就是按下左上角的“汉堡包”图标打开一个导航抽屉。“汉堡包”按钮是可见的,当我开始从左侧拉动时,我会看到按钮上的动画,但按下按钮不会像我预期的那样打开/关闭NavigationDrawer。我已经阅读了[谷歌文档][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
,这就是我的工作重点,我的工作是正确处理事件,因为按下按钮时,我没有得到日志语句。如果这个想法不正确,请告诉我
我的布局:
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>
您需要同步抽屉开关:
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
编辑:该代码适用于我(从您的帖子中复制)
公共类TempActivity扩展了AppCompatActivity{
私有操作bardrawertoggle mDrawerToggle;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.temp);
setupDrawer();
}
@凌驾
公共无效OnConfiguration已更改(配置newConfig){
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@凌驾
公共布尔值OnOptionItemSelected(最终菜单项){
如果(MDRAWERTOGLE.onOptionsItemSelected(项目)){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
@凌驾
后期创建时受保护的空(捆绑包savedInstanceState){
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
私有void setupDrawer(){
Toolbar Toolbar=(Toolbar)findViewById(R.id.my_Toolbar);
设置支持操作栏(工具栏);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
抽屉布局mDrawerLayout=(抽屉布局)findViewById(R.id.my_抽屉布局);
mDrawerToggle=newactionBarDrawerToggle(此,mDrawerLayout,工具栏,R.string.drawer\u打开,R.string.drawer\u关闭){
公共无效onDrawerClosed(视图){
super.onDrawerClosed(视图);
InvalidateOptions SMenu();//创建对OnPrepareOptions SMenu()的调用
}
打开图纸上的公共空白(视图抽屉视图){
super.onDrawerOpened(抽屉视图);
InvalidateOptions SMenu();//创建对OnPrepareOptions SMenu()的调用
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
}
如何使用它。对于抽屉切换,您需要将“显示主页为上”设置为false:
getSupportActionBar().setDisplayHomeAsUpEnabled(false)代码>在ActivityMain.xml中,工具栏位于抽屉布局的外部。这就是问题所在。如果希望工具栏与DrawLayout交互,工具栏需要是DrawerLayout的子项
要解决此问题,请将抽屉布局作为活动的根。这是我的建议。有关报价如下:
要添加导航抽屉,请使用
DroperLayout对象作为布局的根视图。内部
抽屉布局,添加一个包含抽屉主要内容的视图
屏幕(抽屉隐藏时的主布局)和另一个
包含导航抽屉内容的视图
因此,基本上,将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开发的。
它看起来真的很好,很容易实现,而且你不必担心太多
因此,如果您找不到解决问题的方法,您可以尝试一下。覆盖onOptionsItemSelected
方法并在下面使用
if(item.getItemId() == android.R.id.home){ // use android.R.id
mDrawerLayout.openDrawer(Gravity.LEFT);
}
使用ActionBarDrawerToggle
并实现public boolean onOptionsItemSelected(菜单项)
工具栏不必位于抽屉布局内,它不太可能是导致此问题的原因toggle.onOptionsItemSelected(item)
查找所选项目的ID,如果它是android.R.ID.home
,则切换抽屉的可见性。因此,工具栏或创建主菜单项的任何视图都可以放置在布局中的任何位置
在您的活动中:
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);
}
只需在“活动选项”菜单的“管理”函数中调用ActionBarDrawerToggle类的options ItemSelected(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);
}
或
覆盖活动的OnOptions ItemSelected,如下所示
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);
}
}
虽然这里的公认答案很有效,但正如他们所说的“预防胜于治疗”。。添加mDrawerToggle.syncState();在onPostCreate和OnConfiguration Changed()中,与上面@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方法中调用它一次,就不会再调用。我的错误是,我后来用另一种方法调用了它。在经历了许多不同的问题后,我找到了一种方法,将工具栏小部件放入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>
**
**
.... 你的布局。。。
@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);
}
<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>
@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
}
override fun onSupportNavigateUp(): Boolean {
val navController = this.findNavController(R.id.host_fragment)
return NavigationUI.navigateUp(navController,drawer_layout)
}