动态发现Android应用程序的主题名称 [英] Dynamically find the theme name in Android application

查看:149
本文介绍了动态发现Android应用程序的主题名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个平板电脑应用程序,我更名所以有多个主题,根据用户的类型。

I have a tablet application which I am rebranding so there are multiple themes, based on the type of user.

我想找到目前正在申请,并在该主题基于主题的名称,然后我可以做一些后端功能的变化。

I'd like to find the name of the theme that is currently being applied, and based on that theme then I can do some back-end functionality changes.

我要动态地设置一些图像资源,这是很好的,只要我通过在正确的主题资源(R.style.redtheme),但我想,以动态地设置此。

I have to dynamically set some image resources, which is fine as long as I pass in the correct theme resource (the R.style.redtheme), but I'd like to set this dynamically.

TypedArray a = getTheme().obtainStyledAttributes(R.style.redtheme, new int[] {aTabResource.mDrawableAttrId});

要做到造型我创建自定义属性,然后重写他们的风格。

To do the styling I'm creating custom attributes and then overriding them in the styles.

如果没有简单的方式来获得主题,我只是节省preference。

If there is no simple way to get the theme, I will just save a preference.

推荐答案

包管理器可以访问相当多的元数据。

The package manager has access to quite a bit of metadata.

这可以这样来访问:

int theme = 0; //0==not set
try 
{
    String packageName = getClass().getPackage().getName();
    PackageInfo packageInfo = getPackageManager().getPackageInfo(packageName, PackageManager.GET_META_DATA);
    theme = packageInfo.applicationInfo.theme;
}
catch (Exception e) 
{ 
    e.printStackTrace();
}

这运行后,主题将有风格的资源。

After this runs, theme will have the style resource.

这篇关于动态发现Android应用程序的主题名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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