2017-07-31 60 views
0

我想改變我的自定義可繪製形狀純色運行時編程,所以我如何改變這種純色看下面我創建了什麼可繪製的。更改從自定義繪製以編程方式運行時的形狀純色?

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:id="@+id/gradientDrawble" 
     > 
     <selector> 
      <item 
       android:state_selected="true" 
       > 
       <shape 
        android:shape="oval"> 
        <solid 
         android:color="@color/colorCrownCandy"/> 
       </shape> 
      </item> 

      <item > 
       <shape 
        android:shape="oval"> 
        <padding 
         android:bottom="10dp" 
         android:left="10dp" 
         android:right="10dp" 
         android:top="10dp"/> 
        <solid 
         android:color="@color/colorCrownCandy" 
         /> 
       </shape> 
      </item> 
     </selector> 

    </item> 
    <item android:drawable="@drawable/ic_homemenu" 
     /> 
</layer-list> 
+0

@Abhi是的,它是從color.xml的顏色,但我有很多顏色和不同類型的可繪製的,所以我想改變色彩,讓只有1繪製工作所有隻需要改變顏色 – Mahesh

+0

也許[這](https://stackoverflow.com/a/39364523/5015207)會有幫助嗎? – 0X0nosugar

回答

0

試試這個:

// The "view" is the one for which you are applying the drawable as background. 

GradientDrawable backgroundForShape= (GradientDrawable)view.getBackground(); 
backgroundForShape.setColor(COLOR.RED); 

// You can specify the color you want. The color defines in colors.xml 
backgroundForShape.setColor(ContextCompat.getColor(context, R.color.yourColor)); 
相關問題