如何以编程方式获取父主题

编程入门 行业动态 更新时间:2024-10-23 21:39:33
本文介绍了如何以编程方式获取父主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

我想要的东西

从我的代码中,我想以编程方式检查 ,这是我正在使用的主题的父主题,以便区分Sherlock,Sherlock.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.

更多推荐

如何以编程方式获取父主题

本文发布于:2023-11-26 00:06:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631880.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方式   主题

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!