1.用自己的Layout实现layout监听
public class SkinActivity implements Layout{
SkinLayoutInflater skinLayoutInflator;
protected void onCreate(Bundle saveInstanceState){
skinLayoutInflator = new SkinLayoutInflater();
LayoutInflaterCompat.setFactory(getLayoutoutInflater().skinLayoutInflater);
}
public void update(){
SkinManager.loadPath("skin.apk");
SkinManager.update();
}
}
2.实现自己的LayoutInflaterFactory
public class SkinLayoutInflater implements LayoutInflaterFactory{
SkinLayoutInflater skinLayoutInflater;
private
public View createView(View parent,String name,String Context,AttributeSet attrs){
View view = null;
if(name.indexOf(".")!=-1)){//自定义控件
view = createView(context,attrs,name);
}else{//反射构造系统控件
for(String pre:prefixList){
view = createView(context,attrs,pre+name);
if(view == null){break;}
}
}
}
//反射View
public View createView(String name,String Context,AttributeSet attrs){
Class viewClazz = context.getClassLoader().loadClass(name);
Contructor<? extends view> contructor = viewClazz.getContructor(new Class[](Context.class,AttrubuteSet.class));
contructor.setAccessible(true);
}
//对从apk插件中得到的颜色进行应用
public void apply(){
for(SkinItem skinItem :list){
if("textColor".equals(skinItem.getAttrName())){
if(view instanceof TextView){
((TextView)view).setTextColor(skinItem.getId());
}else{
((TextView)view).setTextColor(SkinManager.getInstance().getColor(skinItem.getId()));
}
}
}
}
//skinitem的包装类
class SkinItem{
private String attrName;
private String attrValue;
String String attrType;
int id;
}
private void parseView(Context context,AttributeSet attrs,View view,String name){
for(int i=0;i<attrs.getAttributeCount();i++){
String attrName = attrs.getAttributeName(i);
String attrValue = attrs.getAttributeValue(i);
if("background".equal(attrName)||"textColor".equal(attrName)){
int id = Integer.parseInt(attrValue.substring(1));
String entryName = context.getResources().getResourceEntryName(id);
String typeName = context.getResources().getResourceTypeName(id);
SkinItem skinItem =new SkinItem(attrName,id,entryName,typeName);
list.add(skinItem);//收集换肤的ITem
}
}
if(!list.isEmpty){
SkinView skinview = new SkinView
}
}
class SkinManager{
private Context context;
private Resource skinResource;
private String skinPackageName;
private static SkinManager ourInstance = new SkinManager();
public SkinManager(){
}
public static SkinManager getInstance(){return ourInstance;}
public void init(Context context){
this.context = context.getApplicationContext();
}
//加载apk路径
public void loadSkin(File apkPath){
PackageManager packageManager = coontext.getPackageManager();
PackageInfo packageInfo = packageManager.getPackagerArchiveInfo(apkPath.getAbsolutePath,PackageManager.GET_ACTIVITIE);//这里很重要,根据packageManage来加载apk包信息和包资源
skinPackageName = packageInfo.packageName;
AssetManager assetManager = AssetManager.class.newInstance();
Method addAssetPath()=assetManager.getClass.getClass.getMethod("addAssetPath",String.class);
addAssetPath.invoke(assetManager.apkPath.getAbsoluteFile());
skinResource = new Resource(assetManager,context.getResources().getDisplayMetries,context.getResources().getContext);
}
//对每个View进行更新
public void update(){
for(View view : cacheMap.keySet()){
SkinView skinitem = cacheMap.get(view);
skinItem.apply();
}
}
public int getColor(int resId){
if(skinResource == null){
return resId;
}
String resName = context.getResources().getResoucesEntryName(resId);//得到资源名
int skinId = skinResource.getIndentifier(resName,"color",skinPackageName);//通过资源名去拿到皮肤ID
return skinResource.getColors(resId);
}
}
class SkinView{
private View view;
private List<SkinItem> list;
public SkinView(View view,List<SkinItem> list>) {
this.view = view;
this.list = list;
}
//将ViewItem里面需要换肤的进行更新
public void apply(){
for(SkinItem skinItem :list){
if("textColor".equals(skinItem.getAttrName())){
if(view instanceof TextView){
(TextView)view .setTextColor(skinItem.getId());
}else if(view instanceof Button){
(TextView)viwe.setTextColor(skinItem.getId);
}
}
}
}
}
}