Dynamically find the theme name in Android application

删除回忆录丶 提交于 2019-12-29 07:53:58

问题


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.

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.

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


回答1:


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

It can be accessed like this:

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.



来源:https://stackoverflow.com/questions/9535695/dynamically-find-the-theme-name-in-android-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!