1

我有可拉伸的xml up.xml如以下代码如何动态改变可绘制颜色按钮

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
<item> 
    <rotate 
     android:fromDegrees="45" 
     android:toDegrees="45" 
     android:pivotX="-40%" 
     android:pivotY="87%" > 
     <shape 
      android:shape="rectangle" > 

      <solid 
       android:color="@color/green" /> 
     </shape> 
    </rotate> 
</item> 
</layer-list> 

和我有这样的可绘制附加到按钮如下

<Button android:id="@+id/bill_amount_up" 
    android:layout_width="30dp" 
    android:layout_height="30dp" 
    android:background="@drawable/up" 
    /> 

I” m试图在我的代码中动态改变up.xml文件中固体android:color =“@ color/green”的颜色。我尝试了以下,但它没有奏效。

((GradientDrawable)billAmountUp.getBackground()).setColor(color); 

我收到以下错误 java.lang.ClassCastException:android.graphics.drawable.LayerDrawable不能转换到android.graphics.drawable.GradientDrawable

谁能帮助?谢谢。

+0

https://stackoverflow.com/questions/22001541/android-shape-xml-rotated-drawable-change-color-programmatically – codingnow

回答

1

试试这个我的朋友

Drawable myIcon = getResources().getDrawable(R.drawable.button); 
ColorFilter filter = new LightingColorFilter(Color.BLACK, Color.BLACK); 
myIcon.setColorFilter(filter);