用LayoutInflater多次inflate该xml文件即可。
View v1 = LayoutInflater.from(context).inflate(R.layout.XXX, null);
View v2 = LayoutInflater.from(context).inflate(R.layout.XXX, null);
这里修改v2内容是不会改变v1的。
接下来是故事起由
开始查找的时候被《Android: How to add another layout number of time to my already created LinerLayout?》误导,以为要用
LinearLayout llItem=(LinearLayout)LayoutInflater.createFromSource(R.layout.child, null);
来创建新的资源文件的实例。
可实际上LayoutInflater并没有createFromSource这个方法。于是查找LayoutInflater的API,看到个貌似有复制意思的cloneInContext(Context newContext)
,实验结果和直接用inflate没什么区别(在我用到的范围内)。
如果这篇博文有帮助到你,或者有什么错误提醒,建议改进的地方,欢迎给我留言。