The specified child already has a parent. You must call removeView() on the child's parent first.
出现场景:
要在当前布局上加一个小挂件,常见的两种把 layout 转换成 view 的方式
View view = LayoutInflater.from(context).inflate(R.layout.activity_test_show,parentView,false);(正常)
View view = View.inflate(context, R.layout.activity_test_show, parentView);(报错)
//分析:其实两个大致意思是相同的,最后都是调用的
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean
attachToRoot)
// attachToRoot 永远为 true
View.inflate(context, R.layout.activity_test_show, parentView)
// attachToRoot 自己指定 true or false
LayoutInflater.from(context).inflate(R.layout.activity_test_show,parentView,false);
总结:
Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.
如果是layoutA 要附着在layout的B上(B作为A的子控件) ,那么 attachToRoot 必须钥匙 false 。 剩下的情况暂没发现什么影响