android - 在android中以编程方式改变形状颜色

标签 android android-button android-drawable android-shape

<分区>

我正在尝试以编程方式更改形状的颜色,但它不起作用。

这是形状

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#9F2200"/>
    <stroke android:width="2dp" android:color="#fff" />
</shape>

这是我如何将它用作按钮的背景

<Button                
  android:id="@+id/ibtn_EA_ColorPick_new"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:layout_marginLeft="10dp"
  android:layout_marginRight="10dp"
  android:background="@drawable/round_button"
  />

这是我如何改变它的颜色

GradientDrawable bgShape = (GradientDrawable)btn_ColorPick.getBackground();
bgShape.setColor(Color.RED);

但是当我改变背景颜色时,它会从屏幕上移除按钮。

最佳答案

修改如下代码

GradientDrawable bgShape = (GradientDrawable)btn_ColorPick.getBackground();
 bgShape.mutate()
 bgShape.setColor(Color.RED);

关于android - 在android中以编程方式改变形状颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30253927/

相关文章:

android - 渐变 XML 可绘制中心不起作用

android - Shield 平板电脑中的 IMEI 在 Android Studio 上不可用

android - 如何在 android 中围绕按钮制作动画/持续发光效果?

4G 安卓 FTP

android - 如何在android中为一个按钮设置不同的文字大小?

Android:为按钮添加 NEON 效果

Android 椭圆形 2 种颜色

由于可绘制对象,Android 应用不是 "designed for tablets"

java - 需要一种可中断的方式来监听工作线程中的 UDP 数据包

php - 尝试连接到我的 SQL 数据库但抛出错误