/** * this method should be called in Activity onCreate method, * and before method super.onCreate(savedInstanceState); * * @param activity */ public void attach(AppCompatActivity activity) { if (activity.getDelegate() instanceof LayoutInflaterFactory) { LayoutInflaterFactory originInflaterFactory = (LayoutInflaterFactory) activity.getDelegate(); LayoutInflaterFactory proxyInflaterFactory = (LayoutInflaterFactory) Proxy.newProxyInstance( originInflaterFactory.getClass().getClassLoader(), new Class[]{LayoutInflaterFactory.class}, new InflaterHandler(originInflaterFactory, activity)); LayoutInflater layoutInflater = LayoutInflater.from(activity); LayoutInflaterCompat.setFactory(layoutInflater, proxyInflaterFactory); } }
public void ReplaceSystemControls() { LayoutInflaterCompat.setFactory(LayoutInflater.from(this), new LayoutInflaterFactory() { @Override public View onCreateView(View parent, String name, Context context, AttributeSet attrs) { // /** // * 可以在这里将系统类替换为自定义View,不要在这里做处理,到相应的类中复写该方法 // */ // if (name.equals("ImageView")) { // return new Button(context, attrs); // } return getDelegate().createView(parent, name, context, attrs); } }); }
public void installViewFactory(@Nullable LayoutInflaterFactory delegateFactory) { this.delegateFactory = delegateFactory; LayoutInflater layoutInflater = LayoutInflater.from(activity); if (layoutInflater.getFactory() == null) { LayoutInflaterCompat.setFactory(layoutInflater, this); } else { Log.i(TAG, "The Activity's LayoutInflater already has a Factory installed" + " so we can not install AppCompat's"); } }
private TypefaceCompatFactory(Context context, boolean typefaceDetectionEnabled) { TypefaceCompat.initialize(typefaceDetectionEnabled); try { this.mBaseFactory = (LayoutInflaterFactory) ((AppCompatActivity) context).getDelegate(); } catch (ClassCastException e) { e.printStackTrace(); } }
/** * 初始化夜间控制器 * @param activity 上下文 * @return */ public ChangeModeController init(final Activity activity,final Class mClass){ init(); LayoutInflaterCompat.setFactory(LayoutInflater.from(activity), new LayoutInflaterFactory() { @Override public View onCreateView(View parent, String name, Context context, AttributeSet attrs) { View view = null; try { if(name.indexOf('.') == -1){ if ("View".equals(name)) { view = LayoutInflater.from(context).createView(name, "android.view.", attrs); } if (view == null) { view = LayoutInflater.from(context).createView(name, "android.widget.", attrs); } if (view == null) { view = LayoutInflater.from(context).createView(name, "android.webkit.", attrs); } }else{ if (view == null){ view = LayoutInflater.from(context).createView(name, null, attrs); } } if(view != null){ // Log.e("TAG", "name = " + name); for (int i = 0; i < attrs.getAttributeCount(); i++) { // Log.e("TAG", attrs.getAttributeName(i) + " , " + attrs.getAttributeValue(i)); if (attrs.getAttributeName(i).equals(ATTR_BACKGROUND)) { mBackGroundViews.add(new AttrEntity<View>(view,getAttr(mClass,attrs.getAttributeValue(i)))); } if (attrs.getAttributeName(i).equals(ATTR_TEXTCOLOR)) { mOneTextColorViews.add(new AttrEntity<TextView>((TextView)view,getAttr(mClass,attrs.getAttributeValue(i)))); } if (attrs.getAttributeName(i).equals(ATTR_TWO_TEXTCOLOR)) { mOneTextColorViews.add(new AttrEntity<TextView>((TextView)view,getAttr(mClass,attrs.getAttributeValue(i)))); } if (attrs.getAttributeName(i).equals(ATTR_THREE_TEXTCOLOR)) { mOneTextColorViews.add(new AttrEntity<TextView>((TextView)view,getAttr(mClass,attrs.getAttributeValue(i)))); } if (attrs.getAttributeName(i).equals(ATTR_BACKGROUND_DRAWABLE)) { mBackGroundDrawableViews.add(new AttrEntity<View>(view,getAttr(mClass,attrs.getAttributeValue(i)))); } } } }catch (Exception e){ e.printStackTrace(); } return view; } }); return this; }
LayoutInflaterFactory getLayoutInflaterFactory() { return this; }
public InflaterHandler(LayoutInflaterFactory inflaterFactory, AppCompatActivity activity) { this.inflaterFactory = inflaterFactory; this.activity = activity; }
/** * 初始化夜间控制器 * @param activity 上下文 * @return */ public ChangeModeController init(final Activity activity, final Class mClass){ init(); LayoutInflaterCompat.setFactory(LayoutInflater.from(activity), new LayoutInflaterFactory() { @Override public View onCreateView(View parent, String name, Context context, AttributeSet attrs) { View view = null; try { if(name.indexOf('.') == -1){ if ("View".equals(name)) { view = LayoutInflater.from(context).createView(name, "android.view.", attrs); } if (view == null) { view = LayoutInflater.from(context).createView(name, "android.widget.", attrs); } if (view == null) { view = LayoutInflater.from(context).createView(name, "android.webkit.", attrs); } }else{ if (view == null){ view = LayoutInflater.from(context).createView(name, null, attrs); } } if(view != null){ // Log.e("TAG", "name = " + name); for (int i = 0; i < attrs.getAttributeCount(); i++) { // Log.e("TAG", attrs.getAttributeName(i) + " , " + attrs.getAttributeValue(i)); if (attrs.getAttributeName(i).equals(ATTR_BACKGROUND)) { mBackGroundViews.add(new AttrEntity<View>(view,getAttr(mClass,attrs.getAttributeValue(i)))); } if (attrs.getAttributeName(i).equals(ATTR_TEXTCOLOR)) { mOneTextColorViews.add(new AttrEntity<TextView>((TextView)view,getAttr(mClass,attrs.getAttributeValue(i)))); } if (attrs.getAttributeName(i).equals(ATTR_TWO_TEXTCOLOR)) { mOneTextColorViews.add(new AttrEntity<TextView>((TextView)view,getAttr(mClass,attrs.getAttributeValue(i)))); } if (attrs.getAttributeName(i).equals(ATTR_THREE_TEXTCOLOR)) { mOneTextColorViews.add(new AttrEntity<TextView>((TextView)view,getAttr(mClass,attrs.getAttributeValue(i)))); } if (attrs.getAttributeName(i).equals(ATTR_BACKGROUND_DRAWABLE)) { mBackGroundDrawableViews.add(new AttrEntity<View>(view,getAttr(mClass,attrs.getAttributeValue(i)))); } } } }catch (Exception e){ e.printStackTrace(); } return view; } }); return this; }