How to change Toolbar Navigation and Overflow Menu icons (appcompat v7)?

后端 未结 11 1783
离开以前
离开以前 2020-11-29 19:11

I am having a hard time with v7 Toolbar. What was once a simple task for ActionBar, now seems overly complex. No matter what style I set, I cannot change either

相关标签:
11条回答
  • 2020-11-29 19:50
    mToolbar.setNavigationIcon(R.mipmap.ic_launcher);
    mToolbar.setOverflowIcon(ContextCompat.getDrawable(this, R.drawable.ic_menu));
    
    0 讨论(0)
  • 2020-11-29 19:52

    if you want to change menu item icons, arrow icon (back/up), and 3 dots icon you can use android:tint

      <style name="ToolbarTheme" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
        <item name="android:tint">@color/your_color</item>
      </style>
    
    0 讨论(0)
  • 2020-11-29 19:54

    For right menu you can do it:

    public static Drawable setTintDrawable(Drawable drawable, @ColorInt int color) {
                drawable.clearColorFilter();
                drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
                drawable.invalidateSelf();
                Drawable wrapDrawable = DrawableCompat.wrap(drawable).mutate();
                DrawableCompat.setTint(wrapDrawable, color);
                return wrapDrawable;
            }
    

    And in your activity

    @Override
     public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.menu_profile, menu);
            Drawable send = menu.findItem(R.id.send);
            Drawable msg = menu.findItem(R.id.message);
            DrawableUtils.setTintDrawable(send.getIcon(), Color.WHITE);
            DrawableUtils.setTintDrawable(msg.getIcon(), Color.WHITE);
            return true;
        }
    

    This is the result:

    0 讨论(0)
  • 2020-11-29 19:59

    which theme you have used in activity add below one line code

    for white

    <style name="AppTheme.NoActionBar">
          <item name="android:tint">#ffffff</item>
      </style>
    
    or 
     <style name="AppThemeName" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:tint">#ffffff</item>
    </style>
    

    for black

       <style name="AppTheme.NoActionBar">
          <item name="android:tint">#000000</item>
      </style>
    
    or 
     <style name="AppThemeName" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:tint">#000000</item>
    </style>
    
    0 讨论(0)
  • 2020-11-29 20:03

    To change the navigation icon you can use:

    Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
    setSupportActionBar(toolbar);
    toolbar.setNavigationIcon(R.drawable.my_icon);
    

    To change the overflow icon you can use the method:

    toolbar.setOverflowIcon(ContextCompat.getDrawable(this, R.drawable.ic_my_menu);
    

    If you would like to change the color of the icons you can use:

    with a Material Components Theme (with a MaterialToolbar for example):

    <com.google.android.material.appbar.MaterialToolbar
        android:theme="@style/MyThemeOverlay_Toolbar"
        ...>
    
      <style name="MyThemeOverlay_Toolbar" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
        <!-- color used by navigation icon and overflow icon -->
        <item name="colorOnPrimary">@color/myColor</item>
      </style>
    

    With an AppCompat Theme:

    <android.support.v7.widget.Toolbar
      app:theme="@style/ThemeToolbar" />
    
    
    <style name="ThemeToolbar" parent="Theme.AppCompat.Light">
    
       <!-- navigation icon color -->
       <item name="colorControlNormal">@color/my_color</item>
    
        <!-- color of the menu overflow icon -->
        <item name="android:textColorSecondary">@color/my_color</item>
    </style>
    

    You can also change the overflow icon overriding in the app theme the actionOverflowButtonStyle attribute:

    With a Material Components Theme:

    <style name="AppTheme.Base" parent="Theme.MaterialComponents.DayNight">
        <item name="actionOverflowButtonStyle">@style/OverFlow</item>
    </style>
    
    <style name="OverFlow" parent="Widget.AppCompat.ActionButton.Overflow">
        <item name="srcCompat">@drawable/my_overflow_menu</item>
    </style>
    

    With an AppCompat theme:

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light">
        <item name="actionOverflowButtonStyle">@style/OverFlow</item>
    </style>
    
    <style name="OverFlow" parent="Widget.AppCompat.ActionButton.Overflow">
        <item name="srcCompat">@drawable/my_overflow_menu</item>
    </style>
    
    0 讨论(0)
  • 2020-11-29 20:05

    To change color for options menu items you can

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.your_menu, menu)
    
        menu?.forEach {
            it.icon.setTint(Color.your_color)
        }
    
        return true
    }
    
    0 讨论(0)
提交回复
热议问题