Android 如何以编程方式获取父主题
假设我在Android 如何以编程方式获取父主题,android,android-theme,Android,Android Theme,假设我在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:actionBarTab
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>
此外,我们还可以使用以下方法获取主题的资源id:
int themeResId = appContext.getApplicationInfo().theme;
我想要什么 从我的代码中,我想以编程方式检查这是我正在使用的主题的父主题,以便区分
Sherlock
,Sherlock.Light
&Sherlock.Light.DarkActionBar
在上面的例子中,我想知道我使用的是夏洛克主题的光线变化
注意:您可能想知道,如果我在xml中声明了父项,为什么我需要检查它。原因是我处于一个我实际上不知道的特殊情况下,但这超出了这个问题的范围
您可以使用
Theme myThemeParent = appContext.getTheme().getClass().getSuperclass();
并将其与Sherlock.getclass()进行比较,以验证它是否为父级。其他比较也是如此
Theme myTheme = appContext.getTheme();
Theme myThemeParent = appContext.getTheme().getClass().getSuperclass();