改变形状纯色在运行时作为背景绘制对象XML中 [英] Change shape solid color at runtime inside Drawable xml used as background

查看:106
本文介绍了改变形状纯色在运行时作为背景绘制对象XML中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可绘制的XML文件(background.xml):

I've a Drawable xml file (background.xml):

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape>
         ...........
        </shape>
    </item>

    <item android:id="@+id/shape_id">
        <shape android:shape="rectangle">
            <solid android:color="#ffefefef" /> 
        </shape>
    </item>

</layer-list>

使用的LinearLayout中:

used by a LinearLayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/background"
    android:id="@+id/layout_id"
    >

现在我需要根据一定的条件来改变形状 shape_id 纯色在运行时。如何做到这一点?

Now i need to change the shape shape_id solid color at runtime based on some conditions. How to do this?

推荐答案

由我找到的:

    View v = findViewById(R.id.layout_id);

    LayerDrawable bgDrawable = (LayerDrawable)v.getBackground();
    final GradientDrawable shape = (GradientDrawable)   bgDrawable.findDrawableByLayerId(R.id.shape_id);
    shape.setColor(----);

这篇关于改变形状纯色在运行时作为背景绘制对象XML中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆