Android更改导航抽屉的汉堡图标

时间:2020-08-26 07:51:49

标签: android navigation-drawer

我已经看到了几乎所有与之相关的链接,但并未根据我的要求实现它们。我想用我的自定义抽屉图标替换该默认图标。所以这就是为什么我要在这里问问题。帮我更改汉堡包图标。谢谢!

这是我的导航抽屉代码。

public class MainActivity extends AppCompatActivity {

private AppBarConfiguration mAppBarConfiguration;
ImageView backButton;
DrawerLayout drawer;
NavigationView navigationView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    drawer = findViewById(R.id.drawer_layout);
    navigationView = findViewById(R.id.nav_view);
    View header = navigationView.getHeaderView(0);
    backButton = header.findViewById(R.id.back);
    backButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show();
            drawer.closeDrawer(GravityCompat.START);
        }
    });

    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
            .setOpenableLayout(drawer)
            .build();


    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onSupportNavigateUp() {
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    return NavigationUI.navigateUp(navController, mAppBarConfiguration)
            || super.onSupportNavigateUp();
}

@Override
public void onBackPressed() {
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

1 个答案:

答案 0 :(得分:0)

getSupportActionBar().setHomeAsUpIndicator(R.drawable.dashboardicon);//your icon here

尝试