简体   繁体   English

如何在Drawable中更改形状颜色?

[英]How to change shape colors in Drawable?

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: 其中包括以下背景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)??? 如何以编程方式更改环和内圈的颜色(我需要在Touch事件中执行此操作)???

Late reply, but I figured out that you can access each layer in a LayerList using FindDrawableByLayerId (). 延迟回复,但我发现你可以使用FindDrawableByLayerId ()访问LayerList中的每一层。 Then I have access to each object and set the appropriate color! 然后我可以访问每个对象并设置适当的颜色!

It is possible by having different images in drawable. 通过在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. 考虑一下,如果要将绿色更改为黑色,则需要在drawable中使用黑色圆圈然后尝试此代码。 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);
   }
}

您可以尝试使用ColorStateList ,我认为它符合您的目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM