Cambia Articoli per codice Java
-
12-11-2019 - |
Domanda
Ho il seguente 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>
.
Voglio cambiare "CustomPlayerProgress" e "CustomPlayerProgressSecondary" del codice Java.Come può essere realizzato?
Soluzione
Supponendo che tu voglia accedere a loro attraverso il codice Java dopo che sono stati caricati come disegnabili nella tua app, dovresti essere in grado di fare qualcosa come quanto segue:
@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 */
}
.
Guarda anche questo per alcune altre idee.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow