2013-12-09 78 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);