在Android应用程序中动态查找主题名称

时间:2012-03-02 15:18:07

标签: android android-theme

我有一个平板电脑应用程序,我正在进行品牌重塑,因此根据用户类型有多个主题。

我想找到当前正在应用的主题的名称,然后根据该主题,我可以进行一些后端功能更改。

我必须动态设置一些图像资源,只要我传入正确的主题资源(R.style.redtheme)就可以了,但我想动态设置它。

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

要进行样式设置,我正在创建自定义属性,然后在样式中覆盖它们。

如果没有简单的方法来获取主题,我只会保存一个偏好。

1 个答案:

答案 0 :(得分:8)

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

可以像这样访问:

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();
}

运行后,主题将包含样式资源。