Java 如何在运行时使用onCreateView而不是onCreate更改应用程序的主题
所以我现在有这个代码来更改我的应用程序的主题-Java 如何在运行时使用onCreateView而不是onCreate更改应用程序的主题,java,android,android-fragments,android-theme,appcompatactivity,Java,Android,Android Fragments,Android Theme,Appcompatactivity,所以我现在有这个代码来更改我的应用程序的主题- public class SettingsActivity extends AppCompatActivity{ public static int themeCheck = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (themeCheck == 1){
public class SettingsActivity extends AppCompatActivity{
public static int themeCheck = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (themeCheck == 1){
setTheme(R.style.Dark);
}
else{
setTheme(R.style.Light);
}
setContentView(R.layout.fragment_settings);
}
// some other code changing other stuff
我和我班上的另一个人一起做这个项目,他在编写实际的骨架代码,结果发现他使用了片段,没有将活动扩展到AppCompat。因此,我必须将代码与之合并的类如下所示-
public class SettingsFrament extends android.support.v4.app.Fragment
implements MainActivityFragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = localInflater.inflate(R.layout.fragment_settings,
container, false);
getActivity().setTitle(getString(R.string.settings_fragment_title));
return rootView;
//This rootView and other bits that that guy gave me is redirecting the app
//to my activity from the menu bar that he has.
我想知道如何将我的代码与这个类合并。(我不必合并它,我可以写下代码,只是不知道如何在onCreateView中使用setTheme和其他方法。)
任何帮助都将不胜感激。请在onCreateView回调中尝试此功能
final Context theme1 = new ContextThemeWrapper(getActivity(), R.style.yourCustomTheme1);
final Context theme2 = new ContextThemeWrapper(getActivity(), R.style.yourCustomTheme2);
LayoutInflater localInflater;
getActivity().setTitle(getString(R.string.settings_fragment_title));
if (themeCheck == 1){
localInflater = inflater.cloneInContext(theme1);
}
else{
localInflater = inflater.cloneInContext(theme2);
}
View rootView = localInflater.inflate(R.layout.fragment_settings,
container, false);
return rootView;
您是否尝试过
getActivity().setTheme()
?,并且必须重新启动Activity也要检查