如何以编程方式获取父主题 [英] How to get the parent theme programmatically

查看:76
本文介绍了如何以编程方式获取父主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在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>

从应用程序上下文中,我们可以使用

Theme myTheme = appContext.getTheme();

获取当前应用的Theme类.

Theme myTheme = appContext.getTheme();

而且,我们可以使用以下方法获取主题的资源ID:

int themeResId = appContext.getApplicationInfo().theme;


我想要的东西

从我的代码中,我想以编程方式检查 ,这是我正在使用的主题的父主题,以便区分SherlockSherlock.Light和& Sherlock.Light.DarkActionBar.

在上面的示例中,我想知道我正在使用Sherlock主题的Light变体.

注意: 您可能想知道,如果我在xml中声明了父级,为什么我需要检查父级.原因是我处于我实际上不知道的特定情况下,但这超出了此问题的范围.

解决方案

 Theme myTheme = appContext.getTheme();

您可以使用

获取其父类.

 Theme myThemeParent = appContext.getTheme().getClass().getSuperclass();

,然后将其与 Sherlock .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 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.

解决方案

 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆