Programmatically change color of shape in layer list

后端 未结 4 2125
礼貌的吻别
礼貌的吻别 2020-12-28 14:24

How can I programmatically change the color (#000000) of a shape in a layer list?

Here is my layer list:



        
4条回答
  •  粉色の甜心
    2020-12-28 14:39

    First of all you need to assign id to you layer-list item.

    
    
          
            
                 
            
        
        
            
                
            
        
    
    

    Then get your shape by id.

    LayerDrawable shape = (LayerDrawable) getResources().getDrawable(R.drawable.your_shape)
    

    And you can change the color of your shape by calling

    shape.setColor(Color.Black); // changing to black color
    

    EDIT

    As getDrawable() has been deprecated. Use the following line of code.

    LayerDrawable shape = (LayerDrawable) ContextCompat.getDrawable(YourActivity.this,R.drawable.your_shape)
    

提交回复
热议问题