How to add a navigation drawer?

后端 未结 4 987
别跟我提以往
别跟我提以往 2021-01-07 06:23

In the following activity, i have a fragment and an image on it. The Fragment is just a darker Action Bar that has a picture on it. I\'m trying to have a left slide menu as

4条回答
  •  孤城傲影
    2021-01-07 06:48

    Try something like this, for the layout file, you just need

    
    
    
    
    
        
    
        
    
    
    

    And the implementation could be as simple as this;

    package com.example;
    
    
    public class NavigationDrawer extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_navigation_drawer);
            //You should remove this if you have no intent of using it
            //And if you uset it, to prevent double actionbars, use a style with no actionbar
            //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);//I like setting custom actionbar
            //setSupportActionBar(toolbar);
    
    
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, "Open drawer", "Close drawer");
            drawer.setDrawerListener(toggle);
            toggle.syncState();
    
            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);
        }
    
        @Override
        public void onBackPressed() {
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
            } else {
                super.onBackPressed();
            }
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.note_home, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            return true;//or super.onOptionsItemSelected, false won't show menu
        }
    
    
        @SuppressWarnings("StatementWithEmptyBody")
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            // Handle navigation view item clicks here.
            int id = item.getItemId();
            switch (id) {
                case R.id.nav_camera:
                    break;
                case R.id.nav_gallery:
                    break;
    
                case R.id.nav_schedule:
                    break;
                case R.id.nav_manage:
                    //do someting silly
                    break;
            }
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            return true;
        }
    }
    

提交回复
热议问题