注意事項:在使用系統主題時,要確保該主題在低版本中存在,否則切換主題失效;
雖然在2.33以下版本中的方法不是太有效率,但是確實可以實現主題切換!
在android 10 以下更換主題成功的例子:
public class ActivityRecreate extends Activity {
int myCurTheme;
@Override
protected void onCreate(Bundle savedInstanceState) {
if (getIntent() != null) {
myCurTheme = getIntent().getIntExtra("themeId", 0);
switch (myCurTheme) {
case android.R.style.Theme_Black_NoTitleBar:
myCurTheme=android.R.style.Theme_Black;
break;
case android.R.style.Theme_Black://case值和賦值不一樣是為了換主題
myCurTheme=android.R.style.Theme_Black_NoTitleBar;
break;
default:
myCurTheme = android.R.style.Theme_Black;
break;
}
setTheme(myCurTheme);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recreate);
// Watch for button clicks.
Button button = (Button) findViewById(R.id.recreate);
button.setOnClickListener(mRecreateListener);
}
private View.OnClickListener mRecreateListener = new View.OnClickListener() {
public void onClick(View v) {
// recreate();
finish();
Intent i = getIntent();
i.putExtra("themeId",myCurTheme);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ActivityRecreate.this.startActivity(i);
}
};
}
在android 版本11以上實現主題更換:
public class ActivityRecreate extends Activity {
int myCurTheme;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState!= null) {
myCurTheme =savedInstanceState.getInt("themeId");
switch (myCurTheme) {
case android.R.style.Theme_Holo_Light:
myCurTheme=android.R.style.Theme_Black_NoTitleBar_Fullscreen;
break;
case android.R.style.Theme_Black:
myCurTheme=android.R.style.Theme_Black;
break;
default:
myCurTheme = android.R.style.Theme_Holo_Light;
break;
}
setTheme(myCurTheme);
}
setContentView(R.layout.activity_recreate);
// Watch for button clicks.
Button button = (Button) findViewById(R.id.recreate);
button.setOnClickListener(mRecreateListener);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("themeId",myCurTheme);
}
private View.OnClickListener mRecreateListener = new View.OnClickListener() {
public void onClick(View v) {
recreate();
}
};
在android各版本通用的主題切換方法:
public class ActivityRecreate extends Activity {
int myCurTheme;
boolean isSdkVersion10 = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
isSdkVersion10= Build.VERSION.SDK_INT>10?true:false;
if (!isSdkVersion10 && getIntent() != null) {
myCurTheme = getIntent().getIntExtra("themeId", 0);
setThemeValues();
} else if (isSdkVersion10 &&savedInstanceState!=null){
myCurTheme = savedInstanceState.getInt("themeId");
setThemeValues();
}
super.onCreate(savedInstanceState);//在版本2.33以下,該行必須放在設置主題之後(已經在2.33試過);在3.0以上,可以放在設置主題之前(4.21試過);
setContentView(R.layout.activity_recreate);
// Watch for button clicks.
Button button = (Button) findViewById(R.id.recreate);
button.setOnClickListener(mRecreateListener);
}
private void setThemeValues() {
switch (myCurTheme) {
case android.R.style.Theme_Black_NoTitleBar:
myCurTheme=android.R.style.Theme_Black;
break;
case android.R.style.Theme_Black://case值和賦值不一樣是為了換主題
myCurTheme=android.R.style.Theme_Black_NoTitleBar;
break;
default:
myCurTheme = android.R.style.Theme_Black;
break;
}
setTheme(myCurTheme);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (isSdkVersion10) outState.putInt("themeId", myCurTheme);
}
private View.OnClickListener mRecreateListener = new View.OnClickListener() {
public void onClick(View v) {
if (isSdkVersion10) {
recreate();
} else {
finish();
Intent i = getIntent();
i.putExtra("themeId", myCurTheme);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ActivityRecreate.this.startActivity(i);
}
}
};
}
更多
0