点击工具栏上的图标汉堡包无法打开抽屉式导航栏 [英] Clicking hamburger icon on Toolbar does not open Navigation Drawer

查看:363
本文介绍了点击工具栏上的图标汉堡包无法打开抽屉式导航栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 android.support.v7.widget.Toolbar 和所有我试图做的是$ P $打开NavigationDrawer pssing的汉堡包在左上角的图标。在汉堡包按钮是可见的,而当我开始从左边拉我看到的按钮,但pressing按钮不会打开/关闭NavigationDrawer如我所料动画。我也跟着[谷歌文档] [1],仍然是不能够想出解决办法。对不起,任何混乱,下面是的简化的code我目前正在尝试使用:

I have a simple android.support.v7.widget.Toolbar and all I am trying to do is to open a NavigationDrawer by pressing the "hamburger" icon in the top left corner. The "hamburger" button is visible, and when I start to pull from the left I see the animation on the button but pressing the button does not open/close the NavigationDrawer as I expect. I have followed the [Google Documentation][1] and still am not able to figure this out. Sorry for any confusion, below is the simplified code I am currently attempting to use:

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


}

}

和,因为它是运行时,我没有得到任何日志调试语句。

And as it is I do not get any of the log debug statements when running.

这实际上是我有问题: http://stackoverflow.com/a/2663​​6045/1489990 。我跟着这一点,它只是不工作。

This is essentially the issue I have: http://stackoverflow.com/a/26636045/1489990. I've followed this and it just doesn't work.

这是我的理解是,当汉堡包图标pressed setNavigationOnClickListener 被调用,而这正是我注重我的努力是让事件处理不当因为当我preSS的按钮,我没有得到我的日志报表。让我知道这种想法是不正确。 <一href=\"https://developer.android.com/reference/android/widget/Toolbar.html#setNavigationOnClickListener(android.view.View.OnClickListener)\">https://developer.android.com/reference/android/widget/Toolbar.html#setNavigationOnClickListener(android.view.View.OnClickListener)

It is my understanding that setNavigationOnClickListener is called when the hamburger icon is pressed, and this is where I am focusing my efforts is to get the event handled properly because when I press the button I do not get my log statement. Let me know if this idea is incorrect. 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>

推荐答案

请参阅我的回答以一个类似的帖子。

See my answer to a similar post.

基本上,只要跳过 ActionBarDrawerToggle ,如果你不需要它其他的东西(如回调)。您可以删除所有code和刚刚实施的开/关抽屉的行为,像这样的:

Basically, just skip the ActionBarDrawerToggle if you don't need it for other things (like callbacks). You can remove all that code and just implement your open/close drawer behavior, like this:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
       case android.R.id.home:
            mDrawerLayout.openDrawer(GravityCompat.START);  // OPEN DRAWER
            return true;
        ....
    }
    return super.onOptionsItemSelected(item);
}

上面的code段工程无论是否你使用<一个href=\"https://developer.android.com/reference/android/support/design/widget/NavigationView.html\">NavigationView.如果您正在使用导航视图中,在你的NavigationView点击监听器:

The code segment above works whether or not you're using NavigationView. If you are using navigation view, in your NavigationView click listener:

navigationView.setNavigationItemSelectedListener(
    new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            // Handle menu item clicks here.
            drawerLayout.closeDrawers();  // CLOSE DRAWER
            return true;
        }
    });

更新:

此外,请确保您的活动延长 AppCompatActivity (而不是 ActionBarActivity )。请参见 href=\"http://stackoverflow.com/a/30613179/3449044\">我的回答获得更多信息。

Also, make sure that your activity is extending AppCompatActivity (and not ActionBarActivity). See my answer here for more info.

UPDATE2:

我调试修改后的code。尝试这个。现在应该工作。阅读我的内联的意见,看看有什么我更改原因:

I debugged your revised code. Try this. It should work now. Read my inline comments to see what I changed and why:

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {
//        View.OnClickListener,
//        GoogleApiClient.ConnectionCallbacks,
//        GoogleApiClient.OnConnectionFailedListener {


    DrawerLayout mDrawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(toolbar);
        // You were missing this setHomeAsUpIndicator
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        // I removed your toolbar and drawer click listeners.
        // They're not needed to open drawer.
        // The drawer opens from onOptionsItemSelected().

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
        NavigationView n = (NavigationView) findViewById(R.id.nav);
        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()) {
            // THIS IS YOUR DRAWER/HAMBURGER BUTTON
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);  // OPEN DRAWER
                Log.d("NICK", "CWECNEWKVNERIPNVIEWNFVIPEWNVIPEWNVPIEWNVPIEWNVPIEWNVPIRWNVPRWVPO");
                return true;

        }
        return super.onOptionsItemSelected(item);
    }
}

// I also removed your onConfigurationChanged().
// It's not needed since you're no longer using ActionBarDrawerToggle. 

UPDATE3:问题与布局

看着你ActivityMain.xml,我看到您的工具栏是你DrawerLayout之外。你不能做到这一点。 工具栏必须是你的DrawerLayout 的孩子为了使工具栏找到适当的背景。

Looking at your ActivityMain.xml, I see your toolbar is outside of your DrawerLayout. You can't do that. Toolbar needs to be a child of your DrawerLayout in order for the Toolbar to find its proper context.

您需要让DrawerLayout您的活动的根源。这里的<一个href=\"https://developer.android.com/training/implementing-navigation/nav-drawer.html\">documentation,而具体报价为:

You need to make DrawerLayout the root of your activity. Here's the documentation, and the specific quote is:

要添加一个抽屉式导航与申报您的用户界面
  DrawerLayout对象作为布局的根视图。里面的
  DrawerLayout,添加包含的主要内容一个视图
  屏幕(当抽屉被隐藏您的主要布局)和另一
  认为包含导航抽屉的内容。

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看起来像这样:

So basically, change your ActivityMain.xml to look like this:

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer"
    ... >

    <RelativeLayout
        android:id="@+id/d"
        ...>

        <android.support.v7.widget.Toolbar
            android:id="@+id/my_toolbar"
            ... />

        <!-- Your other content goes here -->

    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav"
        ... />

</android.support.v4.widget.DrawerLayout>

希望这会对您的问题护理。

Hopefully, that takes care of your problems.

这篇关于点击工具栏上的图标汉堡包无法打开抽屉式导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆