layout_width gridlayout examples ejemplo dinamico constraint android android-layout

gridlayout - table layout android



¿Cómo obtener la referencia attr en el código? (2)

Estoy buscando obtener la referencia de un atributo a través del código. En mis diseños xml puedo obtener fácilmente el dibujable de referencia de esta manera:

android:background="?attr/listItemBackground"

La referencia de atributo está establecida por mi Tema. Estoy buscando para ver si es posible obtener esa referencia dibujable mediante código.

Puedo solucionar esto creando atributos de estilo y leyendo el valor dentro de una vista personalizada, pero en este caso quiero averiguar si esto es posible sin hacer todo eso. Pensaría que sería posible, pero no he encontrado formas de obtener esa referencia de atributo.

¡Gracias!


¿No deberías usar:

android:background="@drawable/listItemBackground"

Y entonces:

myImageButton.getBackgroundDrawable()

O tal vez no entendí ...


Así es como lo haces:

// Create an array of the attributes we want to resolve // using values from a theme int[] attrs = new int[] { R.attr.listItemBackground /* index 0 */}; // Obtain the styled attributes. ''themedContext'' is a context with a // theme, typically the current Activity (i.e. ''this'') TypedArray ta = themedContext.obtainStyledAttributes(attrs); // To get the value of the ''listItemBackground'' attribute that was // set in the theme used in ''themedContext''. The parameter is the index // of the attribute 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();