关键代码:setTheme(int ID);
注意点:
1.设置主题必须要在setContentView() 之前调用,所以需要写个Intent去重新开启Activity.
2.为了切换主题保证流畅性,需要去处动画效果.调用 overridePendingTransition(0,0);这个方法.具体实现看下面的代码.
public class MyActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); int themeID=getIntent().getIntExtra("themeID",-1); if (themeID!=-1){ setTheme(themeID); } setContentView(R.layout.mainlayout); } public void changeRed(View View) { finish(); overridePendingTransition(0,0); Intent intent = new Intent(); intent.setClass(getApplicationContext(), MyActivity.class); intent.putExtra("themeID",R.style.MyTheme); startActivity(intent); } }