在用作背景的 Drawable xml 中在运行时更改形状纯色 [英] Change shape solid color at runtime inside Drawable xml used as background

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

问题描述

我有一个 Drawable 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(----);

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

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