android - 更改 Android XML 中的形状颜色

标签 android xml

我有一个 android 可绘制对象,我将把它应用到几个 TextView 的背景

<?xml version="1.0" encoding="utf-8"?>
 <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item> 
    <shape android:shape="rectangle">
      <solid android:color="#000000" /> 
    </shape>
  </item>   

    <item android:id="@+id/clr"
        android:left="0.5dp" android:right="0.5dp"  android:top="0.5dp" android:bottom="0.5dp" >  
     <shape android:shape="rectangle"> 
      <solid android:color="#FFF000" />
    </shape>
   </item>    
 </layer-list>

我想以编程方式更改其颜色。我怎样才能做到这一点?

最佳答案

可以这样做

// "cellLabel" background colour of textview or button etc. 
LayerDrawable layers = (LayerDrawable) cellLabel.getBackground();
// drawable item id
GradientDrawable shape = (GradientDrawable) (layers.findDrawableByLayerId(R.id.clr));
shape.setColor(my_color);

关于android - 更改 Android XML 中的形状颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20463082/

相关文章:

php - XML - 转义字符串的正确方法

java - 将 MarkupBuilder 与 Groovy 结合使用...创建 XML 参数时出现问题

android - Android在 “minifyEnabled true”中使用gradle构建遇到错误,

android - 将大量图像加载到内存会导致内存不足异常 - Android

java - 获取信息时暂停代码执行

java - 调用其他类实现的接口(interface)方法时出现空指针异常

java - XML 属性在 *value* 中带有前缀 - 如何判断它是命名空间前缀?

java - JSONObject JSON 数组长度

android - 服务器无法处理您的请求,值不能为空 :soap parsing

android - Espresso 失败时设置构建失败