Android开发实现切换主题及换肤功能示例

所属分类: 软件编程 / Android 阅读数: 49
收藏 0 赞 0 分享

本文实例讲述了Android开发实现切换主题及换肤功能。分享给大家供大家参考,具体如下:

废话不说先看效果:

创建ColorTheme类用于主题更换:

public class ColorTheme {
  AppCompatActivity ap;
  public ColorTheme(AppCompatActivity _ap){ap=_ap;}
  public void updateTheme(int _data){
    String data=Integer.toString(_data);
    FileOutputStream out=null;
    BufferedWriter writer=null;
    try{
      out=ap.openFileOutput("data",Context.MODE_PRIVATE);
      writer=new BufferedWriter(new OutputStreamWriter(out));
      writer.write(data);
    }catch (IOException e){
      e.printStackTrace();
    }finally {
      try {
        if(writer!=null){
          writer.close();
        }
      }catch (IOException e){
        e.printStackTrace();
      }
    }
  }
  public void loadTheme(){
    FileInputStream in=null;
    BufferedReader reader= null;
    StringBuilder content=new StringBuilder();
    try{
      in=ap.openFileInput("data");
      reader=new BufferedReader(new InputStreamReader(in));
      String line="";
      while((line=reader.readLine())!=null){
        content.append(line);
      }
      ap.setTheme(Integer.parseInt(content.toString()));
    }catch (IOException e){
      e.printStackTrace();
    }finally {
      if(reader!=null){
        try{
          reader.close();
        }catch (IOException e){
          e.printStackTrace();
        }
      }
    }
  }
}

在oncreate中调用:

protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  final ColorTheme newTheme = new ColorTheme(this);
  newTheme.loadTheme();
  setContentView(R.layout.activity_main);

重点:

要现在res/value/style中设计主题的样式:

这里是我设的的四种样式:

<?xml version="1.0"?>
  <resources>
  <!-- Base application theme. -->
  -<style parent="Theme.AppCompat.Light.NoActionBar" name="AppTheme">
  <!-- Customize your theme here. -->
  <item name="colorPrimary">@color/colorPrimary</item>
  <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  <item name="colorAccent">@color/colorAccent</item>
  <item name="colorButtonNormal">@color/colorAccent</item>
</style>
  -<style parent="Theme.AppCompat.Light.NoActionBar" name="Blue">
  <!-- Customize your theme here. -->
  <item name="colorPrimary">@color/blue</item>
  <item name="colorPrimaryDark">@color/blue</item>
  <item name="colorAccent">@color/blue</item>
  <item name="colorButtonNormal">@color/blue</item>
</style>
  -<style parent="Theme.AppCompat.Light.NoActionBar" name="Pink">
  <!-- Customize your theme here. -->
  <item name="colorPrimary">@color/pink</item>
  <item name="colorPrimaryDark">@color/pink</item>
  <item name="colorAccent">@color/pink</item>
  <item name="colorButtonNormal">@color/pink</item>
</style>
  -<style parent="Theme.AppCompat.Light.NoActionBar" name="Turquoise">
  <!-- Customize your theme here. -->
  <item name="colorPrimary">@color/turquoise</item>
  <item name="colorPrimaryDark">@color/turquoise</item>
  <item name="colorAccent">@color/turquoise</item>
  <item name="colorButtonNormal">@color/turquoise</item>
</style>
</resources>

别忘了在color里定义的颜色:

<?xml version="1.0" encoding="UTF-8"?>
  <resources>
  <color name="colorText">#ffffffff</color>
  <color name="hintText">#bfffffff</color>
  <color name="colorPrimary">#de4037</color>
  <color name="colorPrimaryDark">#de4037</color>
  <color name="colorAccent">#de4037</color>
  <!--注册界面提示红色-->
  <color name="hintRed">#de4037</color>
  <color name="blue">#1e50a2</color>
  <color name="pink">#fa7299</color>
  <color name="turquoise">#008577</color>
</resources>

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android控件用法总结》、《Android开发入门与进阶教程》、《Android视图View技巧总结》、《Android编程之activity操作技巧总结》、《Android数据库操作技巧总结》及《Android资源操作技巧汇总

希望本文所述对大家Android程序设计有所帮助。

更多精彩内容其他人还在看

Android 自定义球型水波纹带圆弧进度效果(实例代码)

最近小编接到一个这样的需求,需要实现一个圆形水波纹,带进度,两层水波纹需要渐变显示,且外围有一个圆弧进度。今天小编给大家分享实例代码,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

Flutter 实现下拉刷新上拉加载的示例代码

这篇文章主要介绍了Flutter 实现下拉刷新上拉加载的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Windows实现Flutter环境搭建及配置这一篇就够了

这篇文章主要介绍了Windows实现Flutter环境搭建及配置这一篇就够了,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android利用碎片fragment实现底部标题栏(Github模板开源)

Fragment可以作为Activity的组成部分,一个Activity可以有多个Fragment,这篇文章主要介绍了Android利用碎片fragment实现底部标题栏(Github模板开源),需要的朋友可以参考下
收藏 0 赞 0 分享

android studio 的下拉菜单Spinner使用详解

这篇文章主要介绍了android studio 的下拉菜单Spinner使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

解析Android 8.1平台SystemUI 导航栏加载流程

这篇文章主要介绍了Android 8.1平台SystemUI 导航栏加载流程,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信录音功能

这篇文章主要为大家详细介绍了Android仿微信录音功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿微信键盘切换效果

这篇文章主要为大家详细介绍了Android仿微信键盘切换效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android超清晰6.0权限申请AndPermission

这篇文章主要介绍了Android超清晰6.0权限申请AndPermission,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿微信录制语音功能

这篇文章主要介绍了Android仿微信录制语音功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多