1.使用API设置主题
如下所示,在Activity中使用setTheme
setTheme(R.style.MyTheme1);
2.调用API的时机
需要在super.onCreate(savedInstanceState)
之前调用setTheme
3.重新设置主题
要重新设置主题,则需要结束Activity,重新启动才可以
public class MainActivity extends Activity { private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { if(MyApplication.SHOW_THEME_FLAG == MyApplication.SHOW_1_THEME_FLAG){ setTheme(R.style.MyTheme2); }else if(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("ActionBar Title"); tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(MyApplication.SHOW_THEME_FLAG == MyApplication.SHOW_1_THEME_FLAG){ MyApplication.SHOW_THEME_FLAG = MyApplication.SHOW_2_THEME_FLAG; }else if(MyApplication.SHOW_THEME_FLAG == MyApplication.SHOW_2_THEME_FLAG){ MyApplication.SHOW_THEME_FLAG = MyApplication.SHOW_1_THEME_FLAG; } Intent mIntent = getIntent(); finish(); overridePendingTransition(0,0); startActivity(mIntent); } }); } }
4.常用主题style设置
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接