如何获取父主题编程 [英] How to get the parent theme programatically
问题描述
比方说,我有以下的自定义主题中声明的themes.xml
:
<样式名称=Theme.Custom.Light父=@风格/ Theme.Sherlock.Light>
<项目名称=机器人:actionBarTabStyle> @风格/ Widget.Custom.Light.ActionBar.TabView< /项目>
<项目名称=机器人:actionBarTabTextStyle> @风格/ Widget.Custom.Light.ActionBar.TabText< /项目>
<项目名称=机器人:actionMenuTextColor> @彩色/ ab_item_text< /项目>
&LT;项目name=\"android:actionMenuTextAppearance\">@style/TextAppearance.Custom.Light.Widget.ActionBar.Menu</item>
&LT; /风格&GT;
从应用程序上下文,我们能够获得主题
类目前使用的应用
主题的MyTheme = appContext.getTheme();
和也,我们能够用得到主题的资源ID:
INT themeResId = appContext.getApplicationInfo()主题;
我要什么
从我的code,我想检查编程这是我使用,以福尔摩斯$区分主题的父主题C $ C>,
Sherlock.Light
&安培; Sherlock.Light.DarkActionBar
。
在上面的例子中,我想知道,我使用的福尔摩斯题材的光变化。
注意: 你也许会奇怪,为什么我需要检查的家长,如果我在XML宣布它。原因是,我在一个特殊的情况,就是我其实不知道,但是这超出了这个问题的范围。的
主题的MyTheme = appContext.getTheme();
您可以用得到它的父类
主题myThemeParent = appContext.getTheme()的getClass()getSuperclass之()。
和它的福尔摩斯 .getclass()比较,以验证它是否是父。同样,对于其他比较。
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 programatically 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.
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.
这篇关于如何获取父主题编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!