Android 动态解析布局,实现制作多套主题

之前做过一个项目(随心壁纸),主要展示过去每期的壁纸主题以及相应的壁纸,而且策划要求,最好可以动态变换主题呈现方式,这样用户体验会比较好。嗯,好吧,策划的话,咱们也没法反驳,毕竟这样搞,确实很不错。于是开始去研究这方面的东西。

首先,我想到的是照片墙效果,改变图片就能有不同的呈现方式。可是这样的话,文字以及更深层的自定义效果,就无法实现了。然后,思考了下,决定仿照android原生布局文件解析方式,自己去动态解析布局。

先来看下android 原生布局文件解析流程:

第一步:调用LayoutInflater的inflate函数解析xml文件得到一个view,然后来看看inflate函数:

//使用常见的API方法去解析xml布局文件,LayoutInflater layoutInflater = (LayoutInflater)getSystemService();View root = layoutInflater.inflate(R.layout.main, null,false);

第二步:在inflate函数中,获取一个XmlResourceParser来解析xml布局文件,再往下跟inflate(parser, root, attachToRoot):

public View inflate(int resource, ViewGroup root, boolean attachToRoot) {if (DEBUG) System.out.println(“INFLATING from resource: ” + resource);XmlResourceParser parser = getContext().getResources().getLayout(resource);try {return inflate(parser, root, attachToRoot);} finally {parser.close();}}

第三步:inflate函数中会根据布局的节点名创建根视图,接着根据方法中传进来的root参数,判断是否为空,如果不为null,则为该根视图赋予外面父视图的布局参数。接着调用rInflate函数来为根视图添加所有字节点。

public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) {synchronized (mConstructorArgs) {final AttributeSet attrs = Xml.asAttributeSet(parser);Context lastContext = (Context)mConstructorArgs[0];mConstructorArgs[0] = mContext; //该mConstructorArgs属性最后会作为参数传递给View的构造函数View result = root;try {// Look for the root node.int type;while ((type = parser.next()) != XmlPullParser.START_TAG &&type != XmlPullParser.END_DOCUMENT) {// Empty}if (type != XmlPullParser.START_TAG) {throw new InflateException(parser.getPositionDescription()+ “: No start tag found!”);}final String name = parser.getName(); //节点名,即API中的控件或者自定义View完整限定名if (TAG_MERGE.equals(name)) {if (root == null || !attachToRoot) {throw new InflateException(“<merge /> can be used only with a valid “+ “ViewGroup root and attachToRoot=true”);}rInflate(parser, root, attrs, false);} else {// Temp is the root view that was found in the xmlView temp;if (TAG_1995.equals(name)) {temp = new BlinkLayout(mContext, attrs);} else {temp = createViewFromTag(root, name, attrs);}ViewGroup.LayoutParams params = null;if (root != null) {// Create layout params that match root, if suppliedparams = root.generateLayoutParams(attrs);if (!attachToRoot) {// Set the layout params for temp if we are not// attaching. (If we are, we use addView, below)temp.setLayoutParams(params);}}// Inflate all children under temprInflate(parser, temp, attrs, true);(root != null && attachToRoot) {root.addView(temp, params);}(root == null || !attachToRoot) {result = temp;}}} catch (XmlPullParserException e) {//…} finally {// Don’t retain static reference on context.mConstructorArgs[0] = lastContext;mConstructorArgs[1] = null;}return result;}}

第四步:rInflate方法中主要是去递归调用布局文件根视图的子节点。将解析得到的view添加到parentView。

/*** Recursive method used to descend down the xml hierarchy and instantiate* views, instantiate their children, and then call onFinishInflate().*/void rInflate(XmlPullParser parser, View parent, final AttributeSet attrs,boolean finishInflate) throws XmlPullParserException, IOException {final int depth = parser.getDepth();int type;while (((type = parser.next()) != XmlPullParser.END_TAG ||parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {if (type != XmlPullParser.START_TAG) {continue;}final String name = parser.getName();if (TAG_REQUEST_FOCUS.equals(name)) { //处理<requestFocus />标签parseRequestFocus(parser, parent);} (parser.getDepth() == 0) {throw new InflateException(“<include /> cannot be the root element”);}parseInclude(parser, parent, attrs); //解析<include />节点} InflateException(“<merge /> must be the root element”);} View view = new BlinkLayout(mContext, attrs);final ViewGroup viewGroup = (ViewGroup) parent;final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs);rInflate(parser, view, attrs, true);viewGroup.addView(view, params);} else {//根据节点名构建一个View实例对象final View view = createViewFromTag(parent, name, attrs);final ViewGroup viewGroup = (ViewGroup) parent;//调用generateLayoutParams()方法返回一个LayoutParams实例对象,final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs);rInflate(parser, view, attrs, true); //继续递归调用 viewGroup.addView(view, params); //OK,将该View以特定LayoutParams值添加至父View}}if (finishInflate) parent.onFinishInflate(); //完成解析过程,通知..}怀着淡定从容的心态去面对,也就没有了真正意义上的寂寞了。

Android 动态解析布局,实现制作多套主题

相关文章:

你感兴趣的文章:

标签云: