Android layoutInflate. inflate method details, removeView () Error solution, layoutinflate

Source: Internet
Author: User

Android layoutInflate. inflate method details, removeView () Error solution, layoutinflate

Error:

The specified child already has a parent. You must call removeView (). Answer: This error is straightforward, that is, you have a parent View in viewGroup. addView (childView. There are many causes of errors. I will mainly discuss the error caused by mLayoutInflater. inflate (id, rootView, false. (There are two methods, one is two parameters and the other is three parameters ).
Two parameters: the first parameter: the XML file id of layout; the second parameter: Use the Root view in the XML file as the parent View added to the Child View. That is, the layout of the parent View. a: If the rootView is not empty, the return value of the method is rootView (the Child View represented by xml has been added to the rootView). B: If the rootView is empty, the method returns the child View (the Child View represented by xml) directly. The LayoutParams in the XML file is invalid because LayoutParams is used by the parent View represented by the XML. In onMeasure () of the parent View, the size of the Child View is measured based on whether LayoutParams is MATCH_PARENT or WRAP_CONTENT. If there is no parent View, it is unnecessary.) Use mLayoutInflater. inflate (id, rootView); The returned result is actually rootView, And the rootView must already have a parent layout (DecorateView, generated by the system ). Therefore, rootView. addView (mLayoutInflater. inflate (id, rootView) is equivalent to rootView. addView (rootView.
Three parameters: the problem arises. How can we fix the problem of B? The LayoutParams attribute in the XML file is retained without returning the parent View using the method. Two-step rootView! = Null (whatever the object is), the third parameter = false; mLayoutInflater. inflate (id, rootView, false); in this way, you can retain LayoutParams without attach root view.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.