变化与LT;层项目>在Java code项目 [英] Change <layer-item> items by Java code

查看:92
本文介绍了变化与LT;层项目>在Java code项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的XML:

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

<item android:id="@+id/customPlayerProgressBg">
    <shape>
        <gradient
                android:startColor="#FF999999"
                android:endColor="#FF999999"
        />
    </shape>
</item>

<item android:id="@+id/customPlayerProgressSecondary">
    <clip>
        <shape>
            <gradient
                    android:startColor="#FF5C3C68"
                    android:endColor="#FF5C3C68"
            />
        </shape>
    </clip>
</item>
<item
    android:id="@+id/customPlayerProgress"
>
    <clip>
        <shape>
            <gradient
                android:startColor="#FF5C3C68"
                android:endColor="#FF5C3C68"
            />
        </shape>
    </clip>
</item>

</layer-list>

我想通过Java的code改变customPlayerProgress和customPlayerProgressSecondary。这怎么可能实现呢?

I want to change the "customPlayerProgress" and the "customPlayerProgressSecondary" by Java code. How can that be accomplished?

推荐答案

假设你想有通过Java code对它们的访问被加载在你的应用程序被拉伸后,你应该能够做一些事情如下所示:

Assuming that you want to have access to them through Java code after they are loaded as a Drawable in your app, you should be able to do something like the following:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final LayerDrawable ld = (LayerDrawable) getResources().getDrawable(R.drawable.my_drawable);
    final ClipDrawable d1 = (ClipDrawable) ld.findDrawableByLayerId(R.id.customPlayerProgress);
    final ClipDrawable d2 = (ClipDrawable) ld.findDrawableByLayerId(R.id.customPlayerProgressSecondary);

    /* modify ld, d1 and d2 by calling their methods here */
}

另外,也要看看<一href="http://stackoverflow.com/questions/8018435/android-how-to-change-a-layer-list-drawable">this对于一些更多的想法。

Also look at this for some more ideas.

这篇关于变化与LT;层项目&GT;在Java code项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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