前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android开发实现切换主题及换肤功能示例

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

作者头像
砸漏
发布2020-11-05 10:40:29
8370
发布2020-11-05 10:40:29
举报
文章被收录于专栏:恩蓝脚本

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

废话不说先看效果:

创建ColorTheme类用于主题更换:

代码语言:javascript
复制
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中调用:

代码语言:javascript
复制
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  final ColorTheme newTheme = new ColorTheme(this);
  newTheme.loadTheme();
  setContentView(R.layout.activity_main);

重点:

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

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

代码语言:javascript
复制
<?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里定义的颜色:

代码语言:javascript
复制
<?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程序设计有所帮助。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档