Is it possible to change material design icon color from xml in Android?

前端 未结 9 1055
囚心锁ツ
囚心锁ツ 2020-12-05 06:39

I\'m actually trying to use colored icons in my app. I\'ve downloaded the official material design icon pack from here. Now all the icons in this pack are either white, grey

相关标签:
9条回答
  • 2020-12-05 07:16
    <vector android:height="48dp" android:viewportHeight="24.0"
        android:viewportWidth="24.0" android:width="48dp" xmlns:android="http://schemas.android.com/apk/res/android">
        <path android:fillColor="#ca0f0f" android:pathData="M3,5v14c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2V5c0,-1.1 -0.9,-2 -2,-2H5c-1.11,0 -2,0.9 -2,2zm12,4c0,1.66 -1.34,3 -3,3s-3,-1.34 -3,-3 1.34,-3 3,-3 3,1.34 3,3zm-9,8c0,-2 4,-3.1 6,-3.1s6,1.1 6,3.1v1H6v-1z"/>
    </vector>
    

    you change color by changing android:fillColor="#ca0f0f"

    0 讨论(0)
  • 2020-12-05 07:19

    Yes, it's possible and it's simple using this library: https://github.com/jrvansuita/IconHandler

    You can easily call this:

    Icon.on(yourImageView).color(R.color.your_color).icon(R.mipmap.your_icon).put();
    
    0 讨论(0)
  • 2020-12-05 07:22

    Adding to what Murali said, make multiple drawables with different fill colors.

    Then when needed, add to your code:

    imageView.setImageResource(R.drawable.drawable_with_different_color)
    

    NOT: imageView.setBackgroundResource()

    0 讨论(0)
  • 2020-12-05 07:23

    For changing icon color try

    <ImageButton
        android:layout_width="your value"
        android:layout_height="your value"
        /* and so on ... */
    
        android:tint="YourColor"
        />
    

    Note: the tint color is painted ontop of the image, not a replacement color. So tint #80ff0000 on a black image gives you 50 % red on black, not 50 % red on the background. I.e. this is not equivalent to iOS template images.

    0 讨论(0)
  • 2020-12-05 07:23

    Use the following code to modify the colour of your icon from xml

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ...
        android:foregroundTintMode="src_over"
        android:tint="Your colour"
        ...
        app:srcCompat="@android:drawable/ic_menu_slideshow" 
    />
    
    0 讨论(0)
  • 2020-12-05 07:25

    You can use the TintImageView within the appcompat support library and then tinting/coloring the imageview is by simply calling the android:backgroundTint to tint the imageview into one color.


    Xml

    <TintImageView
    android:layout_width=""
    android:layout_height="" 
    android:src=""
    android:backgroundTint="@color/green"/>
    

    or

    <ImageView 
    android:tint="the_color_you_want"/>
    

    Programatically

    ImageView yourImageView = findViewById(...) yourImageView.setColorFilter(Context.getColor(your_color_here))


    So the above xml will tint the imageView to color green, means that it will colorize each pixel of the imageview that are visible to green.

    0 讨论(0)
提交回复
热议问题