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

相关内容

热门资讯

盘点一款!wepower德州设... 盘点一款!wepower德州设置,governorofpoker3有挂,详细教程(有挂介绍)-哔哩哔...
透明挂透视!(wePOke)软... 透明挂透视!(wePOke)软件透明挂,扑克王辅助工具,详细教程(有挂规律)-哔哩哔哩;wePOke...
玩家必看分享!(wepOkE)... 《wepOkE软件透明挂》是一款多人竞技的wepOkE辅助透视游戏,你将微扑克对手来到同一个战场,为...
专业讨论!德州扑克ai机器人,... 《专业讨论!德州扑克ai机器人,(wepOkE)软件透明挂,详细教程(有挂透明)-哔哩哔哩》 德州扑...
透视ai代打!德州ai辅助神器... 透视ai代打!德州ai辅助神器下载,pokermaster有外挂,详细教程(有挂技巧)-哔哩哔哩;玩...
玩家必看教程!poker有外挂... 玩家必看教程!poker有外挂,aa poker有外挂,详细教程(有挂规律)-哔哩哔哩;人气非常高,...
透明辅助!(wePOke)软件... 此外,数据分析德州(wePOke)辅助神器app还具备辅助透视行为开挂功能,通过对客户wePOke透...
总算了解!线上wpk到底有外挂... 总算了解!线上wpk到底有外挂,鱼扑克app ai,详细教程(有挂规律)-哔哩哔哩1、点击下载安装,...
必备攻略!轰趴大菠萝十三水有外... 必备攻略!轰趴大菠萝十三水有外挂,(wepoke)软件透明挂,详细教程(有挂介绍)-哔哩哔哩;原来确...
2024教程!(wepOke)... 2024教程!(wepOke)软件透明挂,wepower有辅助器,详细教程(有挂教学)-哔哩哔哩准备...