Android应⽤动态修改主题的⽅法⽰例
1.使⽤API设置主题
如下所⽰,在Activity中使⽤setTheme
setTheme(R.style.MyTheme1);
2.调⽤API的时机
需要在Create(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);
}
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设置
<style name="MyTheme1" parent="@android:style/Theme.Material">
<!-- 状态栏 -->
<item name="android:colorPrimaryDark">#f00</item>
<!-- ActionBar颜⾊ -->
<item name="android:colorPrimary">#ff0</item>
<!-- 界⾯背景⾊ -->
<item name="android:windowBackground">@color/windowBackgroundColor1</item>
<!-- 导航栏颜⾊ -->
<item name="android:navigationBarColor">#0000ff</item>
</style>
<style name="MyTheme2" parent="@android:style/Theme.Material">
<!-- 状态栏 -->
<item name="android:colorPrimaryDark">#757575</item>
<!-- ActionBar颜⾊ -->
<item name="android:colorPrimary">#03A9F4</item>
<!-- 界⾯背景⾊ -->
<item name="android:windowBackground">@color/windowBackgroundColor2</item>
<!-- 导航栏颜⾊ -->
<item name="android:navigationBarColor">#1976D2</item>
</style>
总结
以上就是这篇⽂章的全部内容了,希望本⽂的内容对⼤家的学习或者⼯作具有⼀定的参考学习价值,谢谢⼤家对的⽀持。如果
你想了解更多相关内容请查看下⾯相关链接

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。