2013-12-09 80 views
1

我有Android的绘制,我要去申请的背景几个TextViews改变形状颜色在Android XML

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
    <shape android:shape="rectangle"> 
     <solid android:color="#000000" /> 
    </shape> 
    </item> 

    <item android:id="@+id/clr" 
     android:left="0.5dp" android:right="0.5dp" android:top="0.5dp" android:bottom="0.5dp" > 
    <shape android:shape="rectangle"> 
     <solid android:color="#FFF000" /> 
    </shape> 
    </item>  
</layer-list> 

我要编程上改变这种颜色。我怎样才能做到这一点?

+1

尝试:http://stackoverflow.com/questions/5940825/android-change-shape-color-in-runtime?rq=1 –

+1

此用户也声称此解决方案的工作原理:http://stackoverflow.com/a/10448441/2157961 – cheezy

+0

谢谢。只需稍作修改就可以解决问题 – Ajitha

回答

1

这是可能做到像这样

// "cellLabel" background colour of textview or button etc. 
LayerDrawable layers = (LayerDrawable) cellLabel.getBackground(); 
// drawable item id 
GradientDrawable shape = (GradientDrawable) (layers.findDrawableByLayerId(R.id.clr)); 
shape.setColor(my_color);