Why does the Android:layout_height attribute in the layout file of Android not work?

Source: Internet
Author: User

Sometimes we configure a layout file that is found after loading is added to our activity, and does not have the properties we set up to lay out, for example, we set the android:layout_margintop= "100dip", But after running the program found a little effect, similar to the layout_height, such as the beginning of the Android:layout_ property settings have no effect, such problems with our use of adapter as a data source when the role of the majority, Because one of the methods in adapter is GetView, this returned view is a load from the XML layout, which is generally as follows:

if (convertview==null) {      convertviewnull);     }   return Convertview;

The problem is in our Layoutinflater.from (Mcontext). Inflate (r.layout.main, null); This code, when using inflate, if the second argument (View root) is null, Then it will not load the layout-related configuration of the topmost layout node in your layout file (that is, attributes that begin with Android:layout_). We can look at the implementation of this method to illustrate, by looking up the source code, the implementation of inflate in this public View inflate (Xmlpullparser parser, ViewGroup root, Boolean Attachtoroot) method is defined in the. One paragraph:

if(Root! =NULL) {           if(DEBUG) {System.out.println ("Creating params from root:" +root); }           //Create layout params that match root, if suppliedparams =Root.generatelayoutparams (attrs); if(!attachtoroot) {               //Set the layout params for temp If we is not//attaching. (If We is, we use AddView, below)temp.setlayoutparams (params); }       }

As you can see, when root is null, the params = Root.generatelayoutparams (attrs) is not executed, and this code is to convert the layout configuration in XML to Layoutparams. In other words, we load our configured layout properties for the layout class (Framelayout, etc.) to control the size, position, alignment, etc. of the view when OnLayout. Take Framelayout as an example and look at its generatelayoutparams (attrs) method.

 Public layoutparams generatelayoutparams (AttributeSet attrs) {          returnnew  Framelayout.layoutparams (GetContext (), attrs);              }

Very simply, constructs a Framelayout.layoutparams class, this class integrates the Marginparams, adds a gravity to its property configuration ...

Here, if you want to customize your own viewgroup, and the ViewGroup has some property settings for the custom control layout, You can extend the layout configuration by integrating the View.marginparams, and then override the Generatelayoutparams method so that the system framework automatically uses that layout to read the layout properties we have configured in XML to control the location of our view.

Based on the above analysis, we must ensure that the root parameter cannot be null when using Layoutinflate's inflate method, in fact, this root is the meaning of the parent view, that is, when you convert the XML into a view, The parent of the view is root, and if you do not want to add the view to the root, then let the third argument attachtoroot to False, or true if you want to add it.

Speaking of this problem, in fact, there are some layout, their parameter configuration to meet certain conditions will work, such as framelayout in the view, you want its leftmargin to take effect, you must specify its layout_gravity as left, Likewise right corresponds to rightmargin.top and bottom. Be careful when you use it, and look at the source code more. Otherwise it will be nameless name, do not know where the problem.

Three lines of ViewGroup

Onmeasure measuring the size of a view

OnLayout control of the layout of the view

Draw draws the view,drawchild to draw the child view

I'm the dividing line of the king of the Land Tiger.

Reference: http://flysnow.iteye.com/blog/1776538

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.