简体   繁体   English

Android导航抽屉三行汉堡图标

[英]Android Navigation Drawer three line hamburger icon

I'm going crazy trying to figure out what I'm doing wrong and tried all the solutions on SO but still seem to be missing something. 我发疯了,试图找出我做错了什么,并尝试了所有关于SO的解决方案,但似乎仍然缺少一些东西。 This is the first time I am creating a navigation drawer and took the help of an online tutorial. 这是我第一次创建导航抽屉并获得在线教程的帮助。

Everything works great, except the icon shows the back arrow instead of the three lines. 一切正常,除了图标显示后退箭头而不是三行。

The first bunch of answers deal with the v4 library tied to the drawable icon which didn't apply to me since I am using the v7 library On the v7, the first answer I tried was to use below, but still remained as arrow. 第一个答案是与绑定到可绘制图标的v4库有关的,因为我正在使用v7库,这对我并不适用。在v7上,我尝试使用的第一个答案是在下面使用,但仍保留为箭头。

 @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            drawerToggle.syncState();
        }

I also tried this trick, but same result: 我也尝试过这个技巧,但结果相同:

drawerLayout.post(new Runnable() {
            @Override
            public void run() {
                drawerToggle.syncState();
            }

I then decided to give up and try to just force the icon and downloaded the ic_drawer icon and used below to just set it and be done, but still no go: 然后,我决定放弃并尝试仅强制使用该图标,并下载了ic_drawer图标,并在下面使用它进行设置并完成操作,但仍然无法进行:

 toolbar.setNavigationIcon(R.drawable.ic_drawer);

I then went so far as to remove everything tied to making the icon to start from scratch, but even after making my code just as below, the back arrow still persists and clicking it opens the navigation drawer: 然后,我甚至删除了与使图标从头开始相关的所有内容,但是即使按如下所示编写了我的代码,后退箭头仍然仍然存在,单击它会打开导航抽屉:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
DrawerLayout drawerLayout=(DrawerLayout) findViewById(R.id.navDrawer);
        drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.nav_open, R.string.nav_close) {
};

Even if I comment out the toolbar and that drawer toggle to have nothing in my onCreate, I am now still stuck with that back arrow. 即使我注释掉了工具栏,并且该抽屉切换在onCreate中没有任何内容,我现在仍然停留在该后退箭头上。 The navigation drawer still works great and opens when I click on it. 导航抽屉仍然可以很好地工作,并在我单击它时打开。 I just don't know where that back arrow is coming from so I can kill it and try to work back the other code to get the three lines to show up. 我只是不知道后退箭头是从哪里来的,所以我可以杀死它并尝试退回其他代码以显示三行代码。

Any suggestion would be greatly appreciated! 任何建议将不胜感激!

Below is the code in full. 下面是完整的代码。 With all the navigation drawer stuff commented out, I can still see the back arrow, and pressing it or swiping opens the fragment perfectly. 注释掉所有导航抽屉中的内容后,我仍然可以看到后退箭头,然后按下它或滑动即可完美地打开片段。 If I take the comments off, its still the same thing 如果我取消评论,那还是一样

public class Main extends AppCompatActivity {


    public ArrayList<String> strTrips = new ArrayList<String>();
    ArrayAdapter<String> adpTrips;
    ActionBarDrawerToggle drawerToggle;

    //String[] strDefaultList={"Click the '+' on the toolbar to add a new trip","","Once you have at least one trip, you select it to view/enter information"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        // Initialize toolbar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        //setSupportActionBar(toolbar);
        //toolbar.setNavigationIcon(R.drawable.ic_drawer);
        //getSupportActionBar().setDisplayHomeAsUpEnabled(true);






       /* DrawerLayout drawerLayout=(DrawerLayout) findViewById(R.id.navDrawer);
        drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.nav_open, R.string.nav_close) {
          @Override
           public void onDrawerOpened(View drawerView) {
               super.onDrawerOpened(drawerView);
               //getActivity().invalidateOptionsMenu();
               //drawerToggle.syncState();
           }

           @Override
           public void onDrawerClosed(View drawerView) {
               super.onDrawerClosed(drawerView);
               //getActivity().invalidateOptionsMenu();
              // drawerToggle.syncState();
           }


       };*/

    /*    drawerLayout.addDrawerListener(drawerToggle);
*/
        //drawerToggle.syncState();
       /* drawerLayout.post(new Runnable() {
            @Override
            public void run() {
                //drawerToggle.syncState();
            }
        });*/




        subLoadTrips();

    }

/*
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        //drawerToggle.syncState();
    }
*/

The manifest file is below. 清单文件在下面。 The activity where I am adding the NavigationDrawer is 'Main'. 我要添加NavigationDrawer的活动是“ Main”。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dimbu.tripx"
    android:versionCode="10"
    android:versionName="1.0">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Main">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!--
             ATTENTION: This was auto-generated to add Google Play services to your project for
             App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information.
        -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity android:name=".AddTrip">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".Main" />

        </activity>
    </application>

</manifest>

The answer was provided by Mike M in comments. 答案由Mike M在评论中提供。

With all the code being correct, his suggestion to clean/rebuild the project worked and then the changes took effect! 在所有代码正确的情况下,他清理/重建项目的建议生效,然后更改生效!

This one line code should help you. 这一行代码应该可以为您提供帮助。 😎☺ 😎☺

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

toolbar is not your app toolbar here! toolbar不是您的应用工具栏!

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);
 toolbar.setNavigationIcon(R.drawable.ic_drawer);

It's just a view. 这只是一个视图。 Instead of this you should get current app toolbar: 取而代之的是,您应该获得当前的应用程序工具栏:

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);
 getSupportActionBar().setIcon(R.drawable.ic_drawer);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM