public class MainActivity extends Activity {
/** 系统自带主题 */
private static final int DEFAULT_THEME = 0;
/** 绿色 */
private static final int GREEN_THEME = 1;
/** 橘黄色 */
private static final int ORANGE_THEME = 2;
public static int current_theme = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
if (current_theme!= -1){
this.setTheme(current_theme);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.bt_theme).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showChooseDialog();
}
});
}
public void changeTheme(int index) {
switch (index) {
case DEFAULT_THEME:
current_theme = R.style.DefaultTheme;
break;
case GREEN_THEME:
current_theme = R.style.GreenTheme;
break;
case ORANGE_THEME:
current_theme = R.style.OrangeTheme;
break;
default:
break;
}
}
protected void reload() {
Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
}
private int mCurrentWhich = 0;
private int mTempWhich;
private void showChooseDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("选择主题");
final String[] items = new String[] {"系统自带1","绿色","橘黄色" };
//显示单选框,参1:单选字符串数组;参2:当前默认选中的位置;参3:选中监听
builder.setSingleChoiceItems(items, mCurrentWhich,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mTempWhich = which;
Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mCurrentWhich = mTempWhich;
changeTheme(mCurrentWhich);
reload();
}
});
builder.setNegativeButton("取消", null);
builder.show();
}
}
Android 动态修改Activity主题
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 转载请注明原创出处,谢谢! GitHub: @Ricco ps:很早以前,在写日历需求的时候,特意留意了一下小米日...
- 主要内容:不使用Intent,启动Activity:通过宿主Activity,接管需要启动的Activity生命周...
- 目录 Android黑科技动态加载(一)之Java中的ClassLoaderAndroid黑科技动态加载(二)之A...
- 问题描述 在Activity创建完成后,根据逻辑隐藏/显示statusbar 解决方案 在setContentVi...