动态发现Android应用程序的主题名称应用程序、名称、发现、动态

2023-09-04 12:41:00 作者:透过骨子里的傲气

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

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.