我有以下 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代码更改“customPlayerProgress”和“customPlayerProgressSecondary”。如何才能做到这一点呢?
假设您希望在将它们作为 Drawable 加载到您的应用程序中后通过 Java 代码访问它们,您应该能够执行如下操作:
@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 */
}
还看this以获得更多想法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)