LayoutInflater拦截View创建,自定义Resource对象
2017-02-17 00:00
387 查看
场景:
1.统一设计Activity风格
2.加载外部资源,替换应用风格
3.不重启Activity的情况下,替换应用风格
4.应用使用系统默认View,开发者进行代码重构,想要增加view的功能,使用自定义View代替,却不想更改布局文件。例如:AppcomptActivity在factory使用新的View替换,实现风格的变换
原理:
1.拦截view创建
1.1创建自定义View
1.2穿件默认View,加载新的资源文件,设置View的属性
1.统一设计Activity风格
2.加载外部资源,替换应用风格
3.不重启Activity的情况下,替换应用风格
4.应用使用系统默认View,开发者进行代码重构,想要增加view的功能,使用自定义View代替,却不想更改布局文件。例如:AppcomptActivity在factory使用新的View替换,实现风格的变换
原理:
1.拦截view创建
1.1创建自定义View
1.2穿件默认View,加载新的资源文件,设置View的属性
class SkinFactory implements LayoutInflater.Factory { private Context context; Resources resources; ArrayList<WeakReference<SkinAttr>> skinAttrs = new ArrayList<WeakReference<SkinAttr>>(); public SkinFactory(Context context, Resources resources) { this.resources = resources; this.context = context; } @Override public View onCreateView(String name, Context context, AttributeSet attrs) { // 创建View-----------------------start View view = null; try { if (-1 == name.indexOf('.')) { 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 { view = LayoutInflater.from(context).createView(name, null, attrs); } } catch (Exception e) { e.printStackTrace(); view = null; } // 创建View-----------------------end if (view == null) { return null; } for (int i = 0; i < attrs.getAttributeCount(); i++) { String attributeName = attrs.getAttributeName(i); String attributeValue = attrs.getAttributeValue(i); //// TODO: 2/17/2017 find attr that can be change and set new attr if (resources != null) applyResource(resources, new SkinAttr(view, attributeName, attributeValue)); } return view; } public void resotreDefault() { for (WeakReference<SkinAttr> skinAttrWeakReference : skinAttrs) { SkinAttr skinAttr = skinAttrWeakReference.get(); if (skinAttr != null && skinAttr.getView() != null) { applyResource(context.getResources(), skinAttr); } } } public void applyResource(Resources resource, SkinAttr skinAttr) { } class SkinAttr { View view; String attributeName; String attributeValue; public SkinAttr(View view, String attributeName, String attributeValue) { this.view = view; this.attributeName = attributeName; this.attributeValue = attributeValue; } public String getAttributeName() { return attributeName; } public View getView() { return view; } public String getAttributeValue() { return attributeValue; } public String getAttributeValueType() { return attributeValue.split("/")[0]; } public String getAttributeValueName() { return attributeValue.split("/")[1]; } } }
相关文章推荐
- 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)
- 用 layoutInflater打气筒创建一个view对象
- Android 漫游之路------ListView入门、Layout的Inflater创建View对象、常用数据适配器
- 采用layoutInflater打气筒创建一个view对象 →ListView
- [android] 采用layoutInflater打气筒创建一个view对象
- Android 采用Layout Inflater创建一个View对象
- 内容提供者ContentProvider →采用layoutInflater打气筒创建一个view对象
- LayoutInflater和inflate()方法的用法 (自定义View中加载xml布局)
- Android -- listview的使用, Inflater打气筒创建View对象,三种数据Adapter
- android 自定义View 三 ---- LayoutInflater
- 自定义View控件详解(一) :Android LayoutInflater原理分析,带你一步步深入了解View
- LinearLayout详解三:LayoutInflater创建View过程分析
- 安卓主activity引用自定义的View——Android LayoutInflater原理分析
- 自定义View学习(一、LayoutInflater)
- 自定义Layout xml文件转换成View对象和Activity关联的实现
- LayoutInflater和inflate()方法的用法 (自定义View中加载xml布局)
- 自定义Layout用ViewGroup+ListView+GridView进行滑动事件拦截
- 自定义组合控件关于LayoutInflater.from(context).inflate(R.layout.view_title, this,true)的问题
- LinearLayout具体解释三:LayoutInflater创建View过程分析
- 不同Context对象创建的LayoutInflater对象也不同