欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android应用动态修改主题的方法示例

程序员文章站 2023-02-15 19:12:43
1.使用api设置主题 如下所示,在activity中使用settheme settheme(r.style.mytheme1); 2.调用api的时机...

1.使用api设置主题

如下所示,在activity中使用settheme

settheme(r.style.mytheme1);

2.调用api的时机

需要在super.oncreate(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);
    }
    super.oncreate(savedinstancestate);
    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>

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接