7

我的可绘制文件夹中有以下文件,名为/drawable/mybkg.xml. 我想给它充气,这样我就可以以编程方式更改颜色。这在Android中可能吗?

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape android:shape="oval" >
            <padding
                android:top="5dp"
                android:bottom="2dp"
                android:left="2dp"
                android:right="2dp" />

            <solid
                android:angle="270"
                android:color="#1bd4f6" />

            <stroke
                android:width="2dp"
                android:color="#121ce5" />
        </shape>
    </item>

    <!-- Foreground -->
    <item>
        <shape android:shape="oval" >

            <solid android:color="#1b90f6" />
        </shape>
    </item>

</layer-list>

我刚才看到了一个叫 LayerDrawable. 有谁知道我如何 LayerDrawable通过编写代码或膨胀现有的 xml 将我的 xml 文件转换为这样的文件?

4

1 回答 1

2

您应该使用该Resources.getDrawable(int id)方法。这是文档:http://developer.android.com/reference/android/content/res/Resources.html#getDrawable(int) 它膨胀了任何类型的可绘制资源。您唯一要做的就是将返回的可绘制对象转换为您需要的最具体的类型(在您的情况下为LayerDrawable)。这是一个例子:

// calling from Activity
LayerDrawable d = (LayerDrawable) getResources().getDrawable(R.drawable.my_drawable);
于 2013-12-26T22:16:19.447 回答