且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

点击工具栏上的图标汉堡包无法打开抽屉式导航栏

更新时间:2022-12-29 07:56:27

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

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段工程无论是否你使用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://***.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您的活动的根源。这里的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.