android 更换皮肤

1.我们在Values文件中创建attr.xml文件,在其中创建自己的需要的自定义属性,
这里是我需要更改的颜色属性
在这里插入图片描述

	<resources>
   	 	<attr name="theme_bg_color" format="reference" />
    	<attr name="selector_img" format="reference"/>
    </resources>

2.在style中自己想要的主题颜色Theme,然后在入口文件AndroidManifest.xml,application中设置主题,
android:theme="@style/YellowTheme"

<style name="YellowTheme" parent="AppBaseTheme">
        <item name="android:windowBackground">@color/gray_bg_f3</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:textColor">@color/text_color</item>
        <item name="android:textSize">@dimen/sp_14</item>
        <item name="theme_bg_color">@color/background</item>
    </style>
    <style name="BlueTheme" parent="AppBaseTheme">
        <item name="android:windowBackground">@color/gray_bg_f3</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:textColor">@color/text_color</item>
        <item name="android:textSize">@dimen/sp_14</item>
        <item name="theme_bg_color">@color/background_32a9fd</item>
    </style>
    <style name="RedTheme" parent="AppBaseTheme">
        <item name="android:windowBackground">@color/gray_bg_f3</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:textColor">@color/text_color</item>
        <item name="android:textSize">@dimen/sp_14</item>
        <item name="theme_bg_color">@color/background_d0171d</item>
    </style>
    <style name="BlackTheme" parent="AppBaseTheme">
        <item name="android:windowBackground">@color/gray_bg_f3</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:textColor">@color/text_color</item>
        <item name="android:textSize">@dimen/sp_14</item>
        <item name="theme_bg_color">@color/background_212121</item>
    </style>
    <style name="CrimsonTheme" parent="AppBaseTheme">
        <item name="android:windowBackground">@color/gray_bg_f3</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:textColor">@color/text_color</item>
        <item name="android:textSize">@dimen/sp_14</item>
        <item name="theme_bg_color">@color/background_f74f2a</item>
    </style>

3.接下来是代码中更换,我是通过保存的方式把当前的主题颜色以名字的方式保存起来的方便判断
在需要的Activity中进行判断更换,推荐创建一个BaseActivity来处理,
调用setTheme()方法来设置当前的主题, setTheme()方法一定要在设置布局之前

		if (StringUtil.isEmpty(backgroundTheme)){
            setTheme(R.style.YellowTheme);//这是默认主题
        }else{
            switch (backgroundTheme){
                case "红色":
                    setTheme(R.style.RedTheme);
                    break;
                case "橙色":
                    setTheme(R.style.CrimsonTheme);
                    break;
                case "黄色":
                    setTheme(R.style.YellowTheme);
                    break;
                case "蓝色":
                    setTheme(R.style.BlueTheme);
                    break;
                case "黑色":
                    setTheme(R.style.BlackTheme);
                    break;
                default:
                    break;
            }
        }

4.这是更改皮肤的Activity,这里注意一下调用recreate()重新创建Activity

package com.linglingyi.com.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dazhanggui.com.R;
import com.linglingyi.com.base.BaseActivity;
import com.linglingyi.com.utils.StorageAppInfoUtil;
import com.linglingyi.com.utils.StorageCustomerInfo02Util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ${tags}
 *
 * @Title: ${enclosing_method}
 * @author:wujun
 */
public class SkinActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private List<SkinModel> list;
    private MyAdapter adapter;
    @Override
    public int initLayout() {
        return R.layout.skin_layout;
    }

    @Override
    public void initData() {

        tvTitle.setText("更换皮肤");
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        list=new ArrayList<>();
        list.add(new SkinModel("#d0171d","红色"));
        list.add(new SkinModel("#f74f2a","橙色"));
        list.add(new SkinModel("#fcd657","黄色"));
        list.add(new SkinModel("#32a9fd","蓝色"));
        list.add(new SkinModel("#212121","黑色"));
        adapter=new MyAdapter(list);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
        }
    }



    private class MyAdapter extends BaseQuickAdapter<SkinModel, BaseViewHolder>{

        public MyAdapter(@Nullable List<SkinModel> data) {
            super(R.layout.item_skin,data);
        }

        @Override
        protected void convert(final BaseViewHolder helper, final SkinModel item) {
            LinearLayout background=helper.getView(R.id.ll_background);
            TextView type=helper.getView(R.id.tv_type);
            helper.setText(R.id.tv_name,item.getName());
            background.setBackgroundColor(Color.parseColor(item.getBackground()));
            String skinPosition= StorageAppInfoUtil.getInfo("skinPosition",context);
            String position = helper.getPosition()+"";
            if (!position.equals(skinPosition)){
                type.setText("选择");
                type.setBackground(null);
            }
            type.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (item.getName()){
                        case "红色":
                            StorageAppInfoUtil.putInfo(context,"backgroundTheme","红色");
                           StorageAppInfoUtil.putInfo(context,"skinPosition",helper.getPosition()+"");
                            goLogin();
                            break;
                        case "橙色":
                            context.setTheme(R.style.CrimsonTheme);
                            StorageAppInfoUtil.putInfo(context,"backgroundTheme","橙色");
                           StorageAppInfoUtil.putInfo(context,"skinPosition",helper.getPosition()+"");        
                            goLogin();
                            break;
                        case "黄色":
                            StorageAppInfoUtil.putInfo(context,"backgroundTheme","黄色");
                           StorageAppInfoUtil.putInfo(context,"skinPosition",helper.getPosition()+"");
                            goLogin();
                            break;
                        case "蓝色":
                            StorageAppInfoUtil.putInfo(context,"backgroundTheme","蓝色");
                           StorageAppInfoUtil.putInfo(context,"skinPosition",helper.getPosition()+"");
                            goLogin();
                            break;
                        case "黑色":
                            StorageAppInfoUtil.putInfo(context,"backgroundTheme","黑色");
                           StorageAppInfoUtil.putInfo(context,"skinPosition",helper.getPosition()+"");                        
                            goLogin();
                            break;
                        default:
                            break;
                    }
                }
            });
        }
    }
    private void goLogin() {
      	recreate();//重新创建
        Intent intent = new Intent(context, HomeNewActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    private class SkinModel implements Serializable{
        private String background;
        private String name;
        private String type;

        public SkinModel(String background, String name) {
            this.background = background;
            this.name = name;
        }

        public String getBackground() {
            return background;
        }

        public void setBackground(String background) {
            this.background = background;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }
    }
}

使用,之后再需要用到的颜色的地方使用我们之前创建的自定义属性颜色就行了
如:在xml中使用 ?attr/theme_bg_color

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/dp_65"
    android:id="@+id/title"
    android:background="?attr/theme_bg_color">
</RelativeLayout>

代码中使用,这里在说一下 我遇到的坑是 setTheme()方法一定要在设置布局之前

 		TypedValue typedValue=new TypedValue();
        context.getTheme().resolveAttribute(R.attr.theme_bg_color,typedValue,true);
        tipTextView.setBackgroundColor(typedValue.resourceId);

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值