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);