Comment obtenir la référence attr dans le code?

Je cherche à obtenir la référence de pointage à partir d’un atsortingbut via le code. Dans mes mises en page xml, je peux facilement obtenir le dessin dessiné référencé comme ceci:

android:background="?attr/listItemBackground" 

La référence d’atsortingbut est définie par mon thème. Je cherche à voir s’il est possible d’obtenir cette référence via le code.

Je peux contourner ce problème en créant un style attr et en lisant la valeur dans une vue personnalisée, mais dans ce cas, je veux savoir si cela est possible sans faire tout cela. Je pense que ce serait possible mais je n’ai pas trouvé de moyens pour obtenir cette référence d’atsortingbut.

Merci!

Voici comment vous le faites:

 // Create an array of the atsortingbutes we want to resolve // using values from a theme int[] attrs = new int[] { R.attr.listItemBackground /* index 0 */}; // Obtain the styled atsortingbutes. 'themedContext' is a context with a // theme, typically the current Activity (ie 'this') TypedArray ta = themedContext.obtainStyledAtsortingbutes(attrs); // To get the value of the 'listItemBackground' atsortingbute that was // set in the theme used in 'themedContext'. The parameter is the index // of the atsortingbute in the 'attrs' array. The returned Drawable // is what you are after Drawable drawableFromTheme = ta.getDrawable(0 /* index */); // Finally, free the resources used by TypedArray ta.recycle(); 

Ne devriez-vous pas utiliser:

android:background="@drawable/listItemBackground"

Et alors:

myImageButton.getBackgroundDrawable()

Ou peut-être que je n’ai pas compris …