android 在各种版本下实现切换主题

注意事项:在使用系统主题时,要确保该主题在低版本中存在,否则切换主题失效;
虽然在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

你可能感兴趣的:(android 在各种版本下实现切换主题)