AndroidStudio中导航菜单在其他活动中不起作用
创始人
2024-08-20 16:00:48
0

在MainActivity.java文件中,在调用其他活动时添加以下代码:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    if (id == android.R.id.home) {
        onBackPressed();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

在xml文件中将导航布局(nav_header.xml)添加到其他活动的布局文件中。在其他活动中,将以下代码添加到onCreate方法中:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
actionBar.setDisplayHomeAsUpEnabled(true);

NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.nav_item_home:
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.nav_item_profile:
                Intent intent_profile = new Intent(getApplicationContext(), ProfileActivity.class);
                startActivity(intent_profile);
                finish();
                break;
            case R.id.nav_item_settings:
                Intent intent_settings = new Intent(getApplicationContext(), SettingsActivity.class);
                startActivity(intent_settings);
                finish();

        }
        drawerLayout.closeDrawers();
        return true;
    }
});

DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.openDrawer, R.string.closeDrawer) {
    @Override
    public void onDrawerClosed(View drawerView) {
        super.onDrawerClosed(drawerView);
    }

    @Override
    public void onDrawerOpened(View drawerView) {
        super.onDrawerOpened(drawerView);
    }
};
drawerLayout.setDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();

相关内容

热门资讯

突发!wepoker模拟器哪个... 突发!wepoker模拟器哪个好用,微乐自建房运势高有什么表现,总结脚本(有挂功能)-哔哩哔哩该软件...
透视教你!破解辅助插件wepo... 透视教你!破解辅助插件wepoker,we-poker辅助软件教程(透视)真是有方法(哔哩哔哩)1、...
透视数据!德普之星辅助器app... 透视数据!德普之星辅助器app,微信微乐陕西小程序破解器,推荐透视(的确有挂)-哔哩哔哩1)德普之星...
透视关于!wpk俱乐部是真的吗... 透视关于!wpk俱乐部是真的吗,微扑克微乐辅助(透视)好像真的是有挂(哔哩哔哩)进入游戏-大厅左侧-...
据权威媒体报道!wpk私人局辅... 据权威媒体报道!wpk私人局辅助是真的吗,微乐自建房运势高有什么表现,专业透视(有挂技巧)-哔哩哔哩...
脚本了解!aapoker辅助器... 脚本了解!aapoker辅助器怎么用,aapoker透视脚本入口(透视)果然真的是有软件(哔哩哔哩)...
据统计!wepoker辅助器,... 据统计!wepoker辅助器,微乐智能插件激活码,揭幕脚本(的确有挂)-哔哩哔哩运wepoker辅助...
相较于以往!pokemomo辅... 相较于以往!pokemomo辅助工具,微乐小程序辅助脚本靠谱的,必备透视(真是有挂)-哔哩哔哩1、完...
脚本总结!德普之星辅助工具如何... 脚本总结!德普之星辅助工具如何打开,德普之星app安卓版破解版(透视)真是有攻略(哔哩哔哩)1、下载...
网友热议!aapoker透视脚... 网友热议!aapoker透视脚本,微乐小程序辅助器免费,开挂脚本(揭秘有挂)-哔哩哔哩;网友热议!a...