【问题标题】:How to change the humberger icon in toolbar?如何更改工具栏中的汉堡图标?
【发布时间】:2016-07-30 11:55:04
【问题描述】:

我正在使用 mikepenz 抽屉库,但我想用我自己的可绘制图标更改默认的汉堡图标和后退箭头图标。

我尝试了很多次,但 我无法用自己的图标更改图标

谁能帮帮我?

new DrawerBuilder()
    .withActivity(this)
    .withTranslucentStatusBar(false)
    .withActionBarDrawerToggle(false)
    .withToolbar(toolbar)
    .addDrawerItems(
        //pass your items here
    )
    .build();

显示汉堡图标的代码:

getSupportActionBar().setDisplayHomeAsUpEnabled(false);
result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(true);

以下是我多次找到的代码,但我也尝试过,但没有成功

Drawable upArrow = getResources().getDrawable(R.drawable.my_drawable);

        actionBar.setHomeAsUpIndicator(upArrow);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true); 

当我搜索时,我也知道如果您在抽屉构建器中传递工具栏,则无法更改图标,所以谁能告诉我该怎么办?

【问题讨论】:

    标签: android toolbar drawer materialdrawer


    【解决方案1】:

    我还没有尝试过使用该库,但请尝试以下操作:

    ActionBar actionBar = getSupportActionBar();
            if (actionBar != null) {
                final Drawable upArrow = getResources().getDrawable(R.drawable.my_drawable);
    
                actionBar.setHomeAsUpIndicator(upArrow);
                actionBar.setDisplayHomeAsUpEnabled(true);
                actionBar.setHomeButtonEnabled(true);
            }
    

    【讨论】:

    • 好吧,顺便说一句,正确格式化您的问题以避免投票失败。
    【解决方案2】:

    根据这个link,您需要从DrawerBuilder 中删除withToolbar(),然后您必须完全自己处理打开/关闭。

    为此,您可以做一些类似的事情

    protected void onCreate(Bundle savedInstanceState) {
            ...
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false);
            toggle.setDrawerIndicatorEnabled(false);
            toggle.setHomeAsUpIndicator(R.drawable.ic_custom_drawer_icon);
            ...
        }
    

    您还必须添加一个工具栏导航点击监听器来监听自定义抽屉图标上的点击事件。

    protected void onCreate(Bundle savedInstanceState) {
            ...
            toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                    if (drawer.isDrawerOpen(GravityCompat.START)) {
                        drawer.closeDrawer(GravityCompat.START);
                    } else {
                        drawer.openDrawer(GravityCompat.START);
                    }
                }
            });
            ...
        }
    

    您可以随时根据需要动态更新图标

    toggle.setHomeAsUpIndicator(R.drawable.ic_new_icon);
    

    希望这会对你有所帮助。

    【讨论】:

      【解决方案3】:

      private DrawerLayout drawerLayout;
      private ActionBarDrawerToggle actionBarDrawerToggle;
      Toolbar toolbar;
      String Drawer_Open,Drawer_Close;
      
      @Override
      
      protected void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      toolbar=(Toolbar)findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
      //set it button icon
      getSuppotActionBar().setDisplayHomeAsUpEnabled(true);
      //set it makes button Clickble
      getSuppotActionBar().setHomeButtonEnabled(true);
      //set your own icon by using this code
      getSuppotActionBar().setHomeAsUpIndicator(R.drawable.my_icon);
      drawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
      actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,Drawer_Open,Drawer_Close);
      drawerLayout.serDrawerListener(actionBarDrawerToggle);
      }
      
      }

      再次,您有任何疑问,请在这里咨询我.....,希望您能解决您的问题...

      【讨论】:

        【解决方案4】:

        修改以下内容试试:

         result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(true);
        

         result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(false);
        

        此禁用库默认图标然后更改图标...

         getSupportActionBar().setHomeAsUpIndicator(R.drawable.my_drawable);
        

        【讨论】: