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>
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值