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();

相关内容

热门资讯

识别九分钟!德州竞技联盟辅助线... 识别九分钟!德州竞技联盟辅助线上德州后台可以操控(本来真的有挂)-知乎1、超多福利:超高返利,海量正...
输赢七分钟!wpk辅助神器aa... 输赢七分钟!wpk辅助神器aapoker有猫腻(都是真的有挂)-今日头条;暗藏猫腻,小编详细说明aa...
打法3分钟!aapoker软件... 打法3分钟!aapoker软件有猫腻cloudpoker辅助器(果真真的有挂)-小红书aapoker...
大厅房七分钟!德州辅助软件欢乐... 大厅房七分钟!德州辅助软件欢乐棋牌有外挂(一贯真的有挂)-百度知乎1、玩家可以在欢乐棋牌有外挂软件透...
辅助3分钟!微扑克ai代打Aa... 辅助3分钟!微扑克ai代打Aapoker app(一般真的有挂)-小红书1、微扑克ai代打ai辅助优...
计算器2分钟!wpk可以检测伙... 计算器2分钟!wpk可以检测伙牌wpk ai检测(一贯真的有挂)-哔哩哔哩1、玩家可以在wpk可以检...
靠谱8分钟!微扑克辅助软件wp... 靠谱8分钟!微扑克辅助软件wpk辅助(原来真的有挂)-小红书微扑克辅助软件辅助器中分为三种模型:微扑...
软件6分钟!fishpoker... 软件6分钟!fishpoker有挂来玩德州扑克约局神器(其实真的有挂)-小红书1、来玩德州扑克约局神...
软件3分钟!微扑克规律德扑之星... 软件3分钟!微扑克规律德扑之星真的太假了(总是真的有挂)-百度知乎1、在德扑之星真的太假了ai机器人...
app一分钟!微扑克有机器人德... app一分钟!微扑克有机器人德州wpk德州有挂(果真真的有挂)-小红书1、用户打开应用后不用登录就可...