2012-09-11 20 views
26

que hemos probado algo como esto, pero me quedé:cómo obtener el color de fondo del tema actual mediante programación

TypedValue typedValue = new TypedValue(); 
if (this.parentActivity.getTheme().resolveAttribute(android.R.attr.windowBackground, typedValue, true)) 
{ 
    // how to get color? 
} 
+9

en androide xml: fondo = "android:? Attr/colorBackground" – ademar111190

+2

I ensayaron y se determinó que androide ':? Attr/colorBackground' corresponde a la' artículo styles.xml' ' @ color/yourColorHere'. –

+0

@ ademar111190 ¡esta debería ser la respuesta! – Alexandr

Respuesta

52

Puede obtener el color de fondo (o Disponibles) del tema actual por:

TypedValue a = new TypedValue(); 
getTheme().resolveAttribute(android.R.attr.windowBackground, a, true); 
if (a.type >= TypedValue.TYPE_FIRST_COLOR_INT && a.type <= TypedValue.TYPE_LAST_COLOR_INT) { 
    // windowBackground is a color 
    int color = a.data; 
} else { 
    // windowBackground is not a color, probably a drawable 
    Drawable d = activity.getResources().getDrawable(a.resourceId); 
} 
+2

Gran respuesta. En caso de que alguien más se tropiece con esto, en Xamarin debe usar Resource.Attribute.primaryAccentColor en lugar de, por ejemplo, Resource.Styleable.MyTheme_primaryAccentColor. Esto probablemente también se aplica al desarrollo nativo de Android. Es decir, hacer referencia al archivo/objeto attr en lugar del tema directamente. No entiendo la diferencia, pero la última parece que sería la elección correcta, pero NO. – pbarranis

4

Usted puede obtener los recursos de su tema por medio de:

TypedArray a = getTheme().obtainStyledAttributes(R.style.ThemeName, new int[] {R.attr.attribute_name});  
int attributeResourceId = a.getResourceId(0, 0); 
+0

Intenté esto: TypedArray a = this.parentActivity.getTheme(). ObtainStyledAttributes (android.R.attr.windowBackground, new int [] {android.R.attr.windowBackground}); \t \t int attributeResourceId = a.getResourceId (0, 0); \t \t \t \t int aaa = this.parentActivity.getResources(). GetColor (attributeResourceId); Pero esto no está funcionando. Obtengo excepción. –

+0

¡Oh! ¿Qué excepción obtienes? – Swayam

+0

09-12 12: 05: 34.864: E/AndroidRuntime (32137): EXCEPCIÓN FATAL: principal 09-12 12: 05: 34.864: E/AndroidRuntime (32137): android.content.res.Resources $ NotFoundException: archivo res /drawable/screen_background_selector_light.xml de la lista de estado de color ID de recurso # 0x10804a8 09-12 12: 05: 34.864: E/AndroidRuntime (32137): \t en android.content.res.Resources.loadColorStateList (Resources.java) 09- 12 12: 05: 34.864: E/AndroidRuntime (32137): \t en android.content.res.Resources.getColor (Resources.java) ... –

1

para su qoustion la forma más fácil es:

TypedValue typedValue = new TypedValue(); 
if (this.parentActivity.getTheme().resolveAttribute(android.R.attr.windowBackground, typedValue, true)) 
{ 
    // how to get color? 
    int colorWindowBackground = typedValue.data;// **just add this line to your code!!** 
} 
Cuestiones relacionadas