How to get the parent theme programmatically

可紊 提交于 2019-12-14 01:26:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!