121 lines
3.9 KiB
Java
121 lines
3.9 KiB
Java
package com.mogo.skin;
|
||
|
||
import android.graphics.Typeface;
|
||
import android.util.AttributeSet;
|
||
import android.view.View;
|
||
import android.widget.TextView;
|
||
|
||
import com.mogo.skin.utils.SkinThemeUtils;
|
||
|
||
import java.util.ArrayList;
|
||
import java.util.List;
|
||
|
||
/**
|
||
* @author donghongyu
|
||
* @description : 属性集合 需要针对哪些属性进行替换
|
||
*/
|
||
public class SkinAttribute {
|
||
|
||
private static final List<String> mAttributes = new ArrayList<>();
|
||
|
||
static {
|
||
mAttributes.add("background");
|
||
mAttributes.add("src");
|
||
|
||
mAttributes.add("textColor");
|
||
mAttributes.add("textColorHint");
|
||
mAttributes.add("drawableLeft");
|
||
mAttributes.add("drawableTop");
|
||
mAttributes.add("drawableRight");
|
||
mAttributes.add("drawableBottom");
|
||
|
||
mAttributes.add("skinTypeface");
|
||
|
||
mAttributes.add("text");
|
||
mAttributes.add("hint");
|
||
}
|
||
|
||
private static final String TAG = "SkinAttribute";
|
||
|
||
/**
|
||
* 记录换肤需要操作的View
|
||
*/
|
||
List<SkinView> mSkinViews = new ArrayList<>();
|
||
|
||
public SkinAttribute() {
|
||
}
|
||
|
||
/**
|
||
* 记录View中哪几个属性需要换肤,找到当前页面的所有属性
|
||
*/
|
||
public void load(View view, AttributeSet attrs) {
|
||
List<SkinPair> skinPairs = new ArrayList<>();
|
||
for (int i = 0; i < attrs.getAttributeCount(); i++) {
|
||
//获得属性名
|
||
String attributeName = attrs.getAttributeName(i);
|
||
//是否符合 需要筛选的属性名
|
||
if (mAttributes.contains(attributeName)) {
|
||
//属性名写法
|
||
//#ffff
|
||
//?attr/xxx
|
||
//@string/xxx
|
||
String attributeValue = attrs.getAttributeValue(i);
|
||
//如果是写死的字符串 #fffff
|
||
if (attributeValue.startsWith("#")) {
|
||
continue;
|
||
}
|
||
//资源id
|
||
int resId = 0;
|
||
//以?开头的表示用属性
|
||
if (attributeValue.startsWith("?")) {
|
||
//attr Id
|
||
int attrId = Integer.parseInt(attributeValue.substring(1));
|
||
//获得 主题 style 中的 对应 attr 的资源id值
|
||
resId = SkinThemeUtils.getResId(view.getContext(), new int[]{attrId})[0];
|
||
} else {
|
||
try {
|
||
// 以@开头的
|
||
// @12343455332
|
||
String resIdStr = attributeValue.substring(1);
|
||
if (isNumeric(resIdStr)) {
|
||
resId = Integer.parseInt(resIdStr);
|
||
}
|
||
} catch (Exception e) {
|
||
// 由于有时候TextView会直接配置文字,而不是用 @string/XXX 所以会导致这里转换异常,车里简单的处理异常跳过即可
|
||
//e.printStackTrace();
|
||
}
|
||
}
|
||
if (resId != 0) {
|
||
//可以被替换的属性
|
||
SkinPair skinPair = new SkinPair(attributeName, resId);
|
||
skinPairs.add(skinPair);
|
||
}
|
||
}
|
||
}
|
||
|
||
//将View与之对应的可以动态替换的属性集合 放入 集合中
|
||
if (!skinPairs.isEmpty() || view instanceof TextView || view instanceof SkinViewSupport) {
|
||
//选择皮肤 更新
|
||
SkinView skinView = new SkinView(view, skinPairs);
|
||
skinView.applySkin();
|
||
mSkinViews.add(skinView);
|
||
}
|
||
}
|
||
|
||
public static boolean isNumeric(String str) {
|
||
if (str == null) {
|
||
return false;
|
||
}
|
||
return str.matches("-?\\d+(\\.\\d+)?"); // 匹配整数或小数
|
||
}
|
||
|
||
/**
|
||
* 换皮肤
|
||
*/
|
||
public void applySkin() {
|
||
for (SkinView mSkinView : mSkinViews) {
|
||
mSkinView.applySkin();
|
||
}
|
||
}
|
||
}
|