2012-09-11 6 views
26

habe ich versucht, so etwas, aber ich fest:wie Hintergrundfarbe von aktuellen Thema zu erhalten programmatisch

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

bei xml android: background = "android: attr/colorbackground" – ademar111190

+2

I getestet und festgestellt, dass 'android: attr/colorBackground' entspricht dem' styles.xml' Punkt ' @ color/yourColorHere'. –

+0

@ ademar111190 das sollte die Antwort sein !!!! – Alexandr

Antwort

52

Sie bekommen die Hintergrundfarbe (oder Drawable) aus dem aktuellen Thema von:

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

Große Antwort. Für den Fall, dass jemand anderes davon stolpert, müssen Sie auf Xamarin Resource.Attribute.primaryAccentColor anstelle von zum Beispiel Resource.Styleable.MyTheme_primaryAccentColor verwenden. Dies gilt wahrscheinlich auch für die native Entwicklung von Android. Das heißt, verweisen Sie direkt auf die Attr-Datei/das Objekt anstelle des Themas. Ich verstehe den Unterschied nicht, aber letzteres scheint die richtige Wahl zu sein, war aber NICHT. – pbarranis

4

Sie können die Ressourcen Ihres Thema erhalten, indem mit:

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

Ich habe es versucht: TypedArray a = this.parentActivity.getTheme(). ErhaltenStyledAttributes (android.R.attr.windowBackground, new int [] {android.R.attr.windowBackground}); \t \t int attributResourceId = a.getResourceId (0, 0); \t \t \t \t int aaa = diese.parentActivity.getResources(). GetColor (attributeResourceId); aber das funktioniert nicht. Ich bekomme eine Ausnahme. –

+0

Oh! Welche Ausnahme bekommst du? – Swayam

+0

09-12 12: 05: 34.864: E/AndroidRuntime (32137): FATALE AUSNAHME: Haupt 09-12 12: 05: 34.864: E/AndroidRuntime (32137): android.content.res.Resources $ NotFoundException: Datei res /drawable/screen_background_selector_light.xml aus der Farbstatusliste Ressourcen-ID # 0x10804a8 09-12 12: 05: 34.864: E/AndroidRuntime (32137): \t bei android.content.res.Resources.loadColorStateList (Resources.java) 09- 12 12: 05: 34.864: E/AndroidRuntime (32137): \t bei android.content.res.Resources.getColor (Resources.java) ... –

1

für Ihre qoustion ist der einfachste Weg:

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!!** 
} 
Verwandte Themen