我想知道应用程序中的Activity应用了哪个主题。
通常我们使用
设置主题setTheme(android.R.style.Theme_Light);
这里我们指定样式,因此我们可以通过编程方式获取为活动准确应用的特定样式类型。
由于
答案 0 :(得分:16)
Context类有一个名为getThemeResId
的好方法,但是它是私有的,因此你需要使用反射。
以下是一个例子:
@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;
}