Android 以编程方式更改活动的主题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11562051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change Activity's theme programmatically
提问by user1462299
In particular cases I need to remove dialog theme from my activity but it doesn't seem to be working. Here's an example
在特定情况下,我需要从我的活动中删除对话框主题,但它似乎不起作用。这是一个例子
First activity:
第一个活动:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
Second activity:
第二个活动:
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme);
setContentView(R.layout.activity_second);
}
Manifest excerpt:
清单摘录:
<activity android:name="SecondActivity" android:theme="@android:style/Theme.Dialog"></activity>
When I run it's still dialog themed.
当我运行时,它仍然是对话框主题。
API10
API10
Thanks.
谢谢。
回答by user1462299
As docssay you have to call setTheme
before any view output. It seems that super.onCreate()
takes part in view
processing.
正如文档所说,您必须setTheme
在任何视图输出之前调用。似乎super.onCreate()
参与view
处理。
So, to switch between themes dynamically you simply need to call setTheme
before super.onCreate
like this:
因此,要在主题之间动态切换,您只需像这样调用setTheme
之前super.onCreate
:
public void onCreate(Bundle savedInstanceState) {
setTheme(android.R.style.Theme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
回答by Bj?rn Kechel
user1462299's response works great, but if you include fragments, they will use the original activities theme. To apply the theme to all fragments as well you can override the getTheme()method of the Context instead:
user1462299 的响应效果很好,但如果您包含Fragments ,它们将使用原始活动主题。要将主题也应用于所有片段,您可以重写Context的 getTheme()方法:
@Override
public Resources.Theme getTheme() {
Resources.Theme theme = super.getTheme();
if(useAlternativeTheme){
theme.applyStyle(R.style.AlternativeTheme, true);
}
// you could also use a switch if you have many themes that could apply
return theme;
}
You do not need to call setTheme() in the onCreate() Method anymore. You are overriding every request to get the current theme within this context this way.
您不再需要在 onCreate() 方法中调用 setTheme() 。您正在以这种方式覆盖在此上下文中获取当前主题的每个请求。
回答by dondondon
I know that i am late but i would like to post a solution here:
Check the full source code here.
This is the code i used when changing theme using preferences..
我知道我迟到了,但我想在这里发表一个解决方案:
检查完整的源代码在这里。
这是我使用首选项更改主题时使用的代码..
SharedPreferences pref = PreferenceManager
.getDefaultSharedPreferences(this);
String themeName = pref.getString("prefSyncFrequency3", "Theme1");
if (themeName.equals("Africa")) {
setTheme(R.style.AppTheme);
} else if (themeName.equals("Colorful Beach")) {
//Toast.makeText(this, "set theme", Toast.LENGTH_SHORT).show();
setTheme(R.style.beach);
} else if (themeName.equals("Abstract")) {
//Toast.makeText(this, "set theme", Toast.LENGTH_SHORT).show();
setTheme(R.style.abstract2);
} else if (themeName.equals("Default")) {
setTheme(R.style.defaulttheme);
}
Please note that you have to put the code before setcontentview..
请注意,您必须将代码放在 setcontentview 之前..
HAPPY CODING!
快乐编码!
回答by Tamim Attafi
This one works fine for me :
这个对我来说很好用:
theme.applyStyle(R.style.AppTheme, true)
theme.applyStyle(R.style.AppTheme, true)
Usage:
用法:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//The call goes right after super.onCreate() and before setContentView()
theme.applyStyle(R.style.AppTheme, true)
setContentView(layoutId)
onViewCreated(savedInstanceState)
}