我想知道应用程序中的 Activity 应用了哪个主题。
一般我们都是通过使用来设置主题
setTheme(android.R.style.Theme_Light);
这里我们指定了样式,这样我们就能够以编程方式获得准确应用于 Activity 的特定样式类型。
谢谢
最佳答案
Context 类有一个很好的方法,叫做 getThemeResId
,但是它是私有(private)的,因此您需要使用反射。
这是一个例子:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Log.e("TAG", "Def theme: " + R.style.AppTheme);
Log.e("TAG", "Light theme: " + android.R.style.Theme_Light);
Log.e("TAG", "Current theme id: " + getThemeId());
setTheme(android.R.style.Theme_Light);
Log.e("TAG", "Current theme id: " + getThemeId());
}
int getThemeId() {
try {
Class<?> wrapper = Context.class;
Method method = wrapper.getMethod("getThemeResId");
method.setAccessible(true);
return (Integer) method.invoke(this);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
关于android - 以编程方式获取应用于 Activity 的主题值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26301345/