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

后端 未结 11 1784
离开以前
离开以前 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 20:06

    if you want to change your icons to a Vector , create a new one. and then in your Activity.java :

    Toolbar toolbar = findViewById(R.id.your_toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    toolbar.setNavigationIcon(R.drawable.your_icon);
    mToolbar.setOverflowIcon(ContextCompat.getDrawable(this, R.drawable.your_icon2));
    

    To change Vector icon Color, go to your Vector XML file.. in this case it will be your_icon.xml, it will look like this :

    <vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0">
    <path
        android:fillColor="@color/Your_Color"
        android:pathData="M15.41,7.41L14,6l-6,6 6,6 1.41,-1.41L10.83,12z"/>
    

    Note that we used these attributes to set the Vector's color :

    android:fillColor="@color/Your_Color"
    

    Edit : You can't use a color from your colors.XML or somewhere else , the color must be decalred directly in the Vector's XML file.. so it will look like this :

    android:fillColor="#FFF"
    
    0 讨论(0)
  • 2020-11-29 20:06

    add your default theme this line;

       <item name="colorControlNormal">@color/my_color</item>
    
    0 讨论(0)
  • 2020-11-29 20:08

    All the above solutions worked for me in API 21 or greater, but did not in API 19 (KitKat). Making a small change did the trick for me in the earlier versions. Notice Widget.Holo instead of Widget.AppCompat

    <style name="OverFlowStyle"    parent="@android:style/Widget.Holo.ActionButton.Overflow">
        <item name="android:src">@drawable/ic_overflow</item>
    </style>
    
    0 讨论(0)
  • 2020-11-29 20:09

    In order to show the icon, use getSupportActionBar().setIcon(R.xxx.xxx)

    In my case the code is:-

    getSupportActionBar().setIcon (R.mipmap.ic_launcher);

    0 讨论(0)
  • 2020-11-29 20:11

    There is a simple, easy and better approach, if we need to change only the color of hamburger/back icon.

    It is better as it changes color only of desired icon, whereas colorControlNormal and android:textColorSecondary might affect other childviews of toolbar as well.

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
    </style>
    
    <style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
        <item name="spinBars">true</item>
        <item name="color">@android:color/white</item>
    </style>
    
    0 讨论(0)
提交回复
热议问题