Android: How to change android:fillcolor with selector in one Vector Drawable xml

后端 未结 3 442
感动是毒
感动是毒 2021-01-12 01:46

Tab Icons: My current method is to create two files (ic_list_selected_24dp.xml and ic_list_unselected_24dp.xml; they are basically the same but only the android:fillCo

3条回答
  •  礼貌的吻别
    2021-01-12 02:03

    Here is the complete list of steps to use a vector asset as tinted icon in a TabItem (which is part of the support design lib). All parts are present in the original question and linked answer, but are easy to miss.

    1. Create the selector. Note that it has to switch the color for state_selected (as included in the question, but not in the answer linked by @cmingmai. There it only states android:state_enabled which is not relevant for tabs):

    res/color/selector_navigation.xml:

    
    
        
        
    
    
    1. Adjust the vector drawable by adding android:tintMode and android:tint to the vector tag. In addition for the tinting to work with multiply, the fillColor of the path needs to be set to white!

    res/drawable/ic_tab_list:

    
    
        
    
    
    1. Use vector drawable in Layout – or use to create the tabs in code as shown in the developer guide on Tabs. Note that I also modified the tab indicator color to match the active icon color to follow the material design guidelines.

    Relevant part of the layout:

    
    
        
        
    
    
    1. Adjust build.gradle with following to activate vector support for old Android versions (in case it was not already previously added):

      android { defaultConfig { vectorDrawables.useSupportLibrary = true } }

提交回复
热议问题