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

相关内容

热门资讯

德扑之星辅助器购买!德州ai软... 德扑之星辅助器购买!德州ai软件是有样,德扑ai本来是真的有挂(详细起手牌胜率图教程)是一款可以让一...
wpk发牌逻辑!德州wpk德州... wpk发牌逻辑!德州wpk德州真的,WPk原来真的有挂,详细教程,(有挂技巧)1、让任何用户在无需w...
wpk俱乐部作弊!微扑克有辅助... wpk俱乐部作弊!微扑克有辅助挂,微扑克发牌机制(详细辅助挂教程);一、微扑克有辅助挂AI软件牌型概...
wepoke有辅助挂!we辅助... wepoke有辅助挂!we辅助poker德之星(透视辅助)We辅poker助(其实真的有挂);致您一...
德扑ai智能!德扑之星可以看底... 1、德扑ai智能!德扑之星可以看底牌,德扑ai其实是有挂(详细ai人工智能教程);代表性(透视辅助软...
红龙扑克作弊器!扑克世界辅助,... 红龙扑克作弊器!扑克世界辅助,德扑之星是有机制(详细辅助教程);原来确实真的有挂(需添加指定薇757...
wpk德州透视辅助!wPK好像... wpk德州透视辅助!wPK好像真的有挂,WPK都是真的有挂,wpk教程(有挂揭秘);1、全新机制【W...
wepower有外 挂!wep... wepower有外 挂!wepower有辅助软件,WEpoke总是真的有挂,插件教程(有挂细节)是一...
德州之星有外 挂!德扑之星可以... 德州之星有外 挂!德扑之星可以查数据,德扑之星一贯是有挂(详细有辅助挂教程)是一款可以让一直输的玩家...
wepoke插件!wpk系统发... 大家肯定在之前wepoke插件或者wepoke插件中玩过wepoke插件!wpk系统发牌规律,wep...