Java 在onCreate()之后以编程方式更改主题
我的活动顶部有一个菜单项。我想用它来改变按下时的主题。我想在用户启动程序后再这样做,它最终将用于循环浏览一系列不同的主题!现在我只想让它和一个一起工作。我该怎么做 我的回答 主要活动Java 在onCreate()之后以编程方式更改主题,java,android,themes,menuitem,Java,Android,Themes,Menuitem,我的活动顶部有一个菜单项。我想用它来改变按下时的主题。我想在用户启动程序后再这样做,它最终将用于循环浏览一系列不同的主题!现在我只想让它和一个一起工作。我该怎么做 我的回答 主要活动 所以,我已经理解了你的问题,你可能会考虑去做一个家长活动,并在其中设置一个片段,这样你就可以改变你的片段的主题,其中改变主题的控制来自你的父母活动。下面是如何实现这种行为的 在活动中获取片段容器,并在活动的onCreate函数中启动该容器中的片段。 当您有一个菜单选项按钮来决定要在片段中部署哪个主题时,您可能会考虑
所以,我已经理解了你的问题,你可能会考虑去做一个家长活动,并在其中设置一个片段,这样你就可以改变你的片段的主题,其中改变主题的控制来自你的父母活动。下面是如何实现这种行为的 在活动中获取片段容器,并在活动的onCreate函数中启动该容器中的片段。 当您有一个菜单选项按钮来决定要在片段中部署哪个主题时,您可能会考虑在单击菜单选项时再次执行片段事务替换您的片段。 您可以考虑在SharedPreferences中保存所选主题,以便每次您的片段启动时,可以在从SyrdPoad读取选定的主题之后,通过在片段的OnCeCeTeVIEW函数中设置主题来正确设置主题。 现在,如果您正在考虑如何在片段中设置主题,那么在这方面可能会有所帮助。为了您的方便,我正在从那里复制代码
SharedPreferences pref;
SharedPreferences.Editor editor;
int check;
int newcheck;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pref = getApplicationContext().getSharedPreferences("test", Context.MODE_PRIVATE);
check = pref.getInt("x", 0);
if(check == 0){
setTheme(R.style.AppTheme);
}else{
setTheme(R.style.Theme2);
}
setContentView(R.layout.activity_main);
noteActivity = new NoteActivity();
mListNotes = (ListView) findViewById(R.id.main_listview);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_create: //run NoteActivity in new note mode
startActivity(new Intent(this, NoteActivity.class));
break;
case R.id.action_theme:
pref = getApplicationContext().getSharedPreferences("test", Context.MODE_PRIVATE);
editor = pref.edit();
newcheck = pref.getInt("x",0);
if(newcheck == 0) {
newcheck = 1;
}else if(newcheck == 1){
newcheck = 0;
}
editor.clear();//clears the editor to avoid errors
editor.putInt("x",newcheck);//add in new int
editor.commit();//commit
//restart the activity
Intent i = getIntent();
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();//close activity - avoid crash
startActivity(i);//start activity
//TODO show settings activity
break;
}
return super.onOptionsItemSelected(item);
}
我希望你已经知道了。如果您还有什么需要澄清的,请告诉我 那么你现在面临的问题是什么?主题设置不正确?当我按下按钮时,主题没有改变。我用onCreate方法测试了它,所以主题设置正确。那你为什么要重新开始这个活动呢?您应该只使用setTheme函数,就这样。为此上下文设置基本主题。请注意,在上下文中实例化任何视图之前,例如在调用android.app.Activity.setContentView或android.view.LayoutInflater.inflate之前,应该调用此函数。。。正因为如此。我想如果我回忆起那次活动。。。嗯,也许我可以在onCreate中调用它,然后在按下按钮时使用共享首选项来更改布局?但这不会改变它,因为如果你有多种选择,你如何用新主题重新创建活动…谢谢,但我找到了另一种方法+1我现在唯一的问题是,当我关闭并重新打开程序时,当我试图更改样式/主题时,它会崩溃。
SharedPreferences pref;
SharedPreferences.Editor editor;
int check;
int newcheck;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pref = getApplicationContext().getSharedPreferences("test", Context.MODE_PRIVATE);
check = pref.getInt("x", 0);
if(check == 0){
setTheme(R.style.AppTheme);
}else{
setTheme(R.style.Theme2);
}
setContentView(R.layout.activity_main);
noteActivity = new NoteActivity();
mListNotes = (ListView) findViewById(R.id.main_listview);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_create: //run NoteActivity in new note mode
startActivity(new Intent(this, NoteActivity.class));
break;
case R.id.action_theme:
pref = getApplicationContext().getSharedPreferences("test", Context.MODE_PRIVATE);
editor = pref.edit();
newcheck = pref.getInt("x",0);
if(newcheck == 0) {
newcheck = 1;
}else if(newcheck == 1){
newcheck = 0;
}
editor.clear();//clears the editor to avoid errors
editor.putInt("x",newcheck);//add in new int
editor.commit();//commit
//restart the activity
Intent i = getIntent();
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();//close activity - avoid crash
startActivity(i);//start activity
//TODO show settings activity
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// create ContextThemeWrapper from the original Activity Context with the custom theme
final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.yourCustomTheme);
// clone the inflater using the ContextThemeWrapper
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
// inflate the layout using the cloned inflater, not default inflater
return localInflater.inflate(R.layout.yourLayout, container, false);
}