Change fillColor of a vector in android programmatically

后端 未结 5 2284
情书的邮戳
情书的邮戳 2020-12-15 03:13

I want to edit the fill Color of a vector-file in Android programmatically.

In the xml-file I can set my color with the attribute android:fillColor

5条回答
  •  星月不相逢
    2020-12-15 03:32

    None of these answers worked for changing the color of a vector path inside a drawable at runtime. In fact, I still didn't figure that out, but I think this answer would help a lot of people who are just trying to create and paint a simple shape at runtime.

    I was trying to create a custom border Mvvm binding to customise the border & fill colors of a Button at runtime. For a while I was trying to modify an Android drawable to achieve this but found out that wasn't possible. Eventually I figured out how to do this with GradientDrawable.

    I'm using Xamarin.Android in C# so it does look slightly different from Java.

    GradientDrawable gd = new GradientDrawable();
    gd.SetColor(Color.Red);
    gd.SetCornerRadius(10);
    
    gd.SetStroke(3, Color.White);
    
    view.Background = gd;
    

提交回复
热议问题