Toolbar 的使用

【链接】 https://www.jianshu.com/p/e9130d31ee1f
【链接】 https://www.jianshu.com/p/ae0013a4f71a

【菜单选项点击事件】
【法1】

//设置导航图标、添加菜单点击事件要在setSupportActionBar方法之后 
setSupportActionBar(toolbar); 
toolbar.setNavigationIcon(R.mipmap.ic_drawer_home); 
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { 
     @Override
     public boolean onMenuItemClick(MenuItem item) { 
           switch (item.getItemId()) { 
               case R.id.action_search: 
Toast.makeText(MainActivity.this, "Search !", Toast.LENGTH_SHORT).show(); break; 
case R.id.action_notifications:
 Toast.makeText(MainActivity.this, "Notificationa !", Toast.LENGTH_SHORT).show(); break; 
case R.id.action_settings: 
Toast.makeText(MainActivity.this, "Settings !", Toast.LENGTH_SHORT).show(); break; 
} 
     return true;
 } });

【法2】

 /**
     * 菜单项点击事件
     *
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_settings:
                Toast.makeText(this, "设置", Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_jing:
                Toast.makeText(this, "搜索", Toast.LENGTH_SHORT).show();

                break;
            case R.id.action_share:
                Toast.makeText(this, "分享", Toast.LENGTH_SHORT).show();

                break;
        }
        return true;
    }
    原文作者:zhengLH
    原文地址: https://www.jianshu.com/p/8fa1d87e3fe4
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞