问题
Let's say I have the following custom theme declared in themes.xml
:
<style name="Theme.Custom.Light" parent="@style/Theme.Sherlock.Light">
<item name="android:actionBarTabStyle">@style/Widget.Custom.Light.ActionBar.TabView</item>
<item name="android:actionBarTabTextStyle">@style/Widget.Custom.Light.ActionBar.TabText</item>
<item name="android:actionMenuTextColor">@color/ab_item_text</item>
<item name="android:actionMenuTextAppearance">@style/TextAppearance.Custom.Light.Widget.ActionBar.Menu</item>
</style>
From the application context, we are able to get the Theme
class currently applied using
Theme myTheme = appContext.getTheme();
and also, we are able to get the theme's resource id using:
int themeResId = appContext.getApplicationInfo().theme;
What I want
From my code, I would like to check programmatically which is the parent theme of the theme I'm using in order to differentiate between Sherlock
, Sherlock.Light
& Sherlock.Light.DarkActionBar
.
In the example above, I would like to know that I am using the Light variation of the Sherlock theme.
Note: You may wonder why I need to check the parent if I declared it in the xml. Reason is that I'm in a particular situation in which I actually won't know, but this goes beyond the scope of this question.
回答1:
Theme myTheme = appContext.getTheme();
You can get its parent class using
Theme myThemeParent = appContext.getTheme().getClass().getSuperclass();
and compare it with Sherlock.getclass() to verify if it is the parent. Likewise for other comparisons.
来源:https://stackoverflow.com/questions/20267522/how-to-get-the-parent-theme-programmatically