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

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

程序员文章站 2023-02-15 19:09:12
本文实例讲述了android开发实现切换主题及换肤功能。分享给大家供大家参考,具体如下: 废话不说先看效果: 创建colortheme类用于主题更换: p...

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

废话不说先看效果:

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程序设计有所帮助。