之前做过一个项目(随心壁纸),主要展示过去每期的壁纸主题以及相应的壁纸,而且策划要求,最好可以动态变换主题呈现方式,这样用户体验会比较好。嗯,好吧,策划的话,咱们也没法反驳,毕竟这样搞,确实很不错。于是开始去研究这方面的东西。
首先,我想到的是照片墙效果,改变图片就能有不同的呈现方式。可是这样的话,文字以及更深层的自定义效果,就无法实现了。然后,思考了下,决定仿照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("<merg