在特殊情况下,我需要从我的活动中删除对话框主题,但它似乎不起作用.这是一个例子
第一项活动:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
Run Code Online (Sandbox Code Playgroud)
第二项活动:
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme);
setContentView(R.layout.activity_second);
}
Run Code Online (Sandbox Code Playgroud)
清单摘录:
<activity android:name="SecondActivity" android:theme="@android:style/Theme.Dialog"></activity>
Run Code Online (Sandbox Code Playgroud)
当我运行它仍然是对话主题.
API10
谢谢.
use*_*299 159
正如文档所说,你必须setTheme在任何视图输出之前调用.似乎super.onCreate()参与view处理.
所以,主题之间切换动态,你只需要调用setTheme之前super.onCreate是这样的:
public void onCreate(Bundle savedInstanceState) {
setTheme(android.R.style.Theme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
Run Code Online (Sandbox Code Playgroud)
Bjö*_*hel 35
user1462299的响应效果很好,但是如果包含片段,它们将使用原始活动主题.要将主题应用于所有片段,您可以覆盖 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;
}
Run Code Online (Sandbox Code Playgroud)
您不再需要在onCreate()方法中调用setTheme().您正在以这种方式覆盖在此上下文中获取当前主题的每个请求.
don*_*don 11
我知道我迟到了,但我想在这里发表一个解决方案:
检查完整的源代码在这里.
这是我在使用首选项更改主题时使用的代码.
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);
}
Run Code Online (Sandbox Code Playgroud)
请注意,您必须在setcontentview之前放置代码.
快乐的编码!
这个对我来说效果很好:
theme.applyStyle(R.style.AppTheme, true)
用法:
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)
}
Run Code Online (Sandbox Code Playgroud)
或使用setTheme(R.style.AppTheme_Light):
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//The call goes right after super.onCreate() and before setContentView()
setTheme(R.style.AppTheme_Light)
setContentView(R.layout.activity_main)
}
Run Code Online (Sandbox Code Playgroud)
小智 5
我已经使用这段代码来实现黑暗模式...它对我来说工作得很好...您可以在开关中使用它...监听器...
//setting up Night Mode...
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
//Store current mode in a sharedprefernce to retrieve on restarting app
editor.putBoolean("NightMode", true);
editor.apply();
//restart all the activities to apply changed mode...
TaskStackBuilder.create(getActivity())
.addNextIntent(new Intent(getActivity(), MainActivity.class))
.addNextIntent(getActivity().getIntent())
.startActivities();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
94898 次 |
| 最近记录: |