ChangeSkin
ChangeSkin copied to clipboard
BaseSkinActivity#onCreateView中为什么用反射来创建 view?
try
{
//public View createView
// (View parent, final String name, @NonNull Context context, @NonNull AttributeSet attrs)
if (sCreateViewMethod == null)
{
Method methodOnCreateView = delegate.getClass().getMethod("createView", sCreateViewSignature);
sCreateViewMethod = methodOnCreateView;
}
Object object = sCreateViewMethod.invoke(delegate, parent, name, context, attrs);
view = (View) object;
} catch (NoSuchMethodException e)
{
e.printStackTrace();
} catch (InvocationTargetException e)
{
e.printStackTrace();
} catch (IllegalAccessException e)
{
e.printStackTrace();
}
这里的代码为什么用发射创建而不是用 直接调用delegate.createView 呢?如下
view = delegate.createView(parent, name, context, attrs);
我猜测是作者怕哪个版本的createView方法变成private,就不能调用了,用反射保险一些 不知道猜的对不对,请HongYang兄解惑一下:)
搞不懂为什么要实现createView这个部分,应该只需要解析attrs就好了。createView还是由父类完成就好了? @Override public View onCreateView(View parent, String name, Context context, AttributeSet attrs) { View view = super.onCreateView(parent, name, context, attrs); List<SkinAttr> skinAttrList = SkinAttrSupport.getSkinAttrs(attrs, context); if (skinAttrList.isEmpty()) { return view; } if (view == null) { view = createViewFromTag(context, name, attrs); } injectSkin(view, skinAttrList); return view;
}