How to change shape colors in Drawable?

旧巷老猫 提交于 2019-12-09 06:35:08

问题


I have the following button:

<Button
    android:id="@+id/Button_Collect"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_marginBottom="16dp"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/CollectButtonShape" />

which looks like this:

which consists of the following background drawable:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
  <shape android:innerRadius="0dp" android:shape="ring" android:thicknessRatio="2" android:useLevel="false" >
    <solid android:color="@android:color/transparent" />

    <stroke
        android:width="2dp"
        android:color="#FF27AE60" />
  </shape>
</item>
<item android:top="10dp" android:left="10dp" android:right="10dp" android:bottom="10dp">
    <shape android:shape="oval">
        <solid android:color="#FF27AE60" />
    </shape>
</item>
</layer-list>

How do I programmatically change the colors of the ring and inner circle (I need to do this on the the Touch event)???


回答1:


Late reply, but I figured out that you can access each layer in a LayerList using FindDrawableByLayerId(). Then I have access to each object and set the appropriate color!




回答2:


It is possible by having different images in drawable. Consider, if you want to change the green color into black, you need to have black color circle in drawable and then try this code. This will help you..

On the button click event, use this code

public void onClick(View v) {
   if(v == ButtonName) {
     ButtonName.setImageResource(R.drawable.ImageName);
   }
}



回答3:


You could try using a ColorStateList, it serves the purpose you are after, I think.



来源:https://stackoverflow.com/questions/19547245/how-to-change-shape-colors-in-drawable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!