How can I change the color of a GradientDrawable in Android 21 (Lollipop)?

时间:2015-05-04 19:42:41

标签: android colors runtime textview android-5.0-lollipop

Hopefully you can help me out and help me out fast. So far my application was working fine, but since my phone updated to Lollipop the drawable icons of the lines of the public traffic service are not being coloured properly. Here is how I did it so far:

    item = (TextView) convertView;
    LayerDrawable layers = (LayerDrawable)  context.getResources().getDrawable(R.drawable.shape_circle_gridview_line_element);
    GradientDrawable shape = (GradientDrawable) (layers.findDrawableByLayerId(R.id.item_shape_gridview_line_element_circle_top));
    shape.setColor(context.getResources().getColor(LineColorChooser.getColor(data.get(position).getTypeId())));
    item.setBackgroundDrawable(layers);

Now all the GradientDrawables keep their XML defined values, which looks like this:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/item_shape_gridview_line_element_circle_bottom" >
        <shape
            android:shape="rectangle" >
            <size
                android:width="45dp"
                android:height="45dp" />
            <solid 
                android:color="#00000000"/>
        </shape>
    </item>

    <item
        android:id="@+id/item_shape_gridview_line_element_circle_top"
        android:left="5dp"
        android:right="5dp"
        android:bottom="5dp"
        android:top="5dp">
        <shape
            android:shape="oval" >

            <size
                android:width="35dp"
                android:height="35dp" />
            <solid 
                android:color="#FFFFFFFF"/>

        </shape>
    </item>

</layer-list>

How can I change the colours runtime? I read that this is not possible anymore with Lollipop, is that true?

0 个答案:

没有答案