如何从android中的另一个应用程序资源更改主题? [英] How to change theme from another app resource in android?

查看:55
本文介绍了如何从android中的另一个应用程序资源更改主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有一种方法可以通过在styles.xml中定义并像这样使用它来设置主题

setTheme(android.R.style.MyTheme);

但是,我想从我开发的另一个应用程序中获取主题.我知道资源名称,实际上我可以使用此代码块获取主题 ID;

Resources res = getPackageManager().getResourcesForApplication("com.example.theme");int resThemeId = res.getIdentifier("my_theme","style","com.example.theme");

当我调试时,我看到 resThemeId 不为零.

然后,我需要最后一个命令来设置这个主题.在 super.onCreate() 函数之前,我尝试实现此方法,但似乎不起作用

setTheme(resThemeId);

但不是这个,如果我写下面的语句,我工作正常

setTheme(android.R.style.Theme_Holo_Light);

那么,我应该怎么做才能使用来自不同包资源的主题?

解决方案

那么,我应该怎么做才能使用来自不同包资源的主题?

出于多种原因,您不应该这样做.我写了一个简单的项目,表明只要包包含您的活动使用的资源,这确实是可能的.

参见:https://github.com/jaredrummler/SO-41872033><小时>

基本上,您需要从活动中返回包的资源:

公共类 MainActivity 扩展 AppCompatActivity {资源资源;@Override protected void onCreate(Bundle savedInstanceState) {int themeResId = getResources().getIdentifier("AppTheme", "style", "com.example.theme");如果(themeResId != 0){设置主题(themeResId);}super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Override 公共资源 getResources() {如果(资源 == 空){尝试 {resources = getPackageManager().getResourcesForApplication("com.example.theme");} catch (PackageManager.NameNotFoundException e) {资源 = super.getResources();}}返还资源;}}

这只是为了表明这是可能的.同样,我建议您避免这种情况.

I know there is a way to set themes by defining in styles.xml and use it like that

setTheme(android.R.style.MyTheme);

However, I want to get themes from another app which I developed as well. I know the resources names and actually I am able to get theme id with this code block;

Resources res = getPackageManager().getResourcesForApplication("com.example.theme");
int resThemeId = res.getIdentifier("my_theme","style","com.example.theme");

When I debug, I see that resThemeId is not zero.

Then, I need the final command to set this theme. Before super.onCreate() function, I try to implement this method but it seems it is not working

setTheme(resThemeId);

But instead of this, if I write below statement, I works fine

setTheme(android.R.style.Theme_Holo_Light);

So, what should I do to use a theme from different package resource?

解决方案

So, what should I do to use a theme from different package resource?

You shouldn't do this for many reasons. I wrote a simple project that shows that it is indeed possible as long as the package contains the resources your activity uses.

See: https://github.com/jaredrummler/SO-41872033


Basically, you would need to return the package's resources from the activity:

public class MainActivity extends AppCompatActivity {

  Resources resources;

  @Override protected void onCreate(Bundle savedInstanceState) {

    int themeResId = getResources().getIdentifier("AppTheme", "style", "com.example.theme");
    if (themeResId != 0) {
      setTheme(themeResId);
    }

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }

  @Override public Resources getResources() {
    if (resources == null) {
      try {
        resources = getPackageManager().getResourcesForApplication("com.example.theme");
      } catch (PackageManager.NameNotFoundException e) {
        resources = super.getResources();
      }
    }
    return resources;
  }

}

This is just to show that it is possible. Again, I recommend that you avoid this.

这篇关于如何从android中的另一个应用程序资源更改主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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