【问题标题】:How to change hamburger icon in Android (NavigationDrawer)如何在 Android 中更改汉堡图标(导航抽屉)
【发布时间】:2017-01-21 06:06:08
【问题描述】:

在编写此线程之前,我尝试实现我在 stackoverflow 中找到的不同解决方案,但没有任何工作正常。

我正在开发一个使用自定义导航抽屉的 Android 应用程序,我必须更改操作栏的标准图标(现在是工具栏,对吗?)和设置图标。

这是我的代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitleTextColor(Color.parseColor("#009754"));
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

这就是我尝试实现的:

此解决方案不起作用:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitleTextColor(Color.parseColor("#009754"));
toolbar.setNavigationIcon(R.drawable.ic_draw);
setSupportActionBar(toolbar);

此解决方案不起作用:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false);
toggle.setDrawerIndicatorEnabled(false);
toggle.setHomeAsUpIndicator(R.drawable.ic_custom_drawer_icon);

我不明白为什么我不能更改图标,我不知道是什么问题......

【问题讨论】:

    标签: android android-layout android-actionbar navigation-drawer


    【解决方案1】:

    简单优雅的解决方案

    地点

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

    之后

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer,toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
    

    Navigation Activity 整体解决方案

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer,toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
    
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.dashboardicon);
    
        navigationView= (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    

    注意:不需要任何 setNavigationOnClickListener()

    【讨论】:

      【解决方案2】:

      ActionBarDrawerToggle禁用抽屉指示器:

      toggle.setDrawerIndicatorEnabled(false);
      

      然后:

      toolbar.setNavigationIcon(R.drawable. ic_custom_drawer_icon);
      

      【讨论】:

      • 然后通过在工具栏上调用setNavigationOnClickListener() 来添加自定义侦听器,如提到的here
      【解决方案3】:
      Just use this :
      
      toolbar.post(new Runnable() {
                  @Override
                  public void run() {
                      Drawable d = ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_launcher, null);
                      toolbar.setNavigationIcon(d);
                  }
              });
      

      【讨论】:

        猜你喜欢
        • 2016-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-21
        • 2022-08-15
        • 1970-01-01
        • 2015-12-24
        • 1970-01-01
        相关资源
        最近更新 更多