Android应用动态修改主题的方法示例
1.使用API设置主题
如下所示,在Activity中使用setTheme
setTheme(R.style.MyTheme1);
2.调用API的时机
需要在super.onCreate(savedInstanceState)之前调用setTheme
3.重新设置主题
要重新设置主题,则需要结束Activity,重新启动才可以
publicclassMainActivityextendsActivity{ privateTextViewtv; @Override protectedvoidonCreate(BundlesavedInstanceState){ if(MyApplication.SHOW_THEME_FLAG==MyApplication.SHOW_1_THEME_FLAG){ setTheme(R.style.MyTheme2); }elseif(MyApplication.SHOW_THEME_FLAG==MyApplication.SHOW_2_THEME_FLAG){ setTheme(R.style.MyTheme1); } super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv=findViewById(R.id.tv); getActionBar().show(); getActionBar().setTitle("ActionBarTitle"); tv.setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewview){ if(MyApplication.SHOW_THEME_FLAG==MyApplication.SHOW_1_THEME_FLAG){ MyApplication.SHOW_THEME_FLAG=MyApplication.SHOW_2_THEME_FLAG; }elseif(MyApplication.SHOW_THEME_FLAG==MyApplication.SHOW_2_THEME_FLAG){ MyApplication.SHOW_THEME_FLAG=MyApplication.SHOW_1_THEME_FLAG; } IntentmIntent=getIntent(); finish(); overridePendingTransition(0,0); startActivity(mIntent); } }); } }
4.常用主题style设置
#f00 #ff0 @color/windowBackgroundColor1 #0000ff #757575 #03A9F4 @color/windowBackgroundColor2 #1976D2
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。如果你想了解更多相关内容请查看下面相关链接