Android's OnLayout, layout method explained

Source: Internet
Author: User

The OnLayout method is the layout method of the ViewGroup neutron view, which is used to place the child view. Placing the child view is simple, just rewrite the OnLayout method, and then get an instance of the child view, and call the layout method of the child view to implement it. In practical development, it is generally used in conjunction with the Onmeasure measurement method.

OnLayout Method:

 @Override   protected  abstract  void  onlayout (boolean  changed," span class= "keyword" style= "font-weight:bold" >int  L, int  T, int  R, int  b); 
This method is defined in ViewGroup as an abstract function, inheriting that class must implement the OnLayout method, and ViewGroup onmeasure is not necessarily overridden. The placement of the view is based on a rectangular space, and the l,t,r,b of the onlayout is the free space for the rectangle to place the parent control (except for the margin and padding left, top, and bottom right, and bottom values.

Layout method:

public  void  layout (int  L, int  T, int  R, int  b); 
This method is the placement method of the view, which is implemented in the view class. Calling this method requires passing in the top left, top, and bottom right corner of the rectangle space where the view is placed. these four values are relative to the parent control (not absolute height). For example, if the incoming is (10, 10, 100, 100), the view is displayed at the upper-left corner of the parent control (10, 10), the size of the display is a width of 90 (the parameter r,b is relative to the upper-left corner), which is somewhat like an absolute layout.

Common development used to Relativelayout, LinearLayout, framelayout ... These are the layouts that inherit viewgroup. These layouts are implemented through the implementation of the ViewGroup OnLayout method, but the implementation method is not the same.

Here is a custom ViewGroup demo that uses onlayout and layout to place the sub view horizontally, at intervals of 20px

 Public  class myviewgroup extends viewgroup {  //Sub-view horizontal interval  Private Final Static intpadding = -;     PublicMyviewgroup (context context, AttributeSet Attrs) {    Super(context, attrs);    //TODO auto-generated constructor stub  }  @Override  protected voidOnLayout (BooleanChangedintLintTintRintb) {    //TODO auto-generated method stub        //Dynamic acquisition of child view instances     for(inti =0, size = Getchildcount (); i < size; i++) {      View view = Getchildat (i);      //Place sub view, Width height is      View.layout (L, T, L + -, T + -);      L + = -+ padding;    }      }  }

XML layout of activity:
<relativelayout  xmlns:android  =" Http://schemas.android.com/apk/res/android "   xmlns:tools  ="/http Schemas.android.com/tools "   android:layout_width  =" match_parent "   android:layout_height  =" Match_parent "    android:padding  = "10DP" ;    <  com.example.layout.MyViewGroup  android:layout_width= "match_parent"       android:layout_height= "100DP    " android:background= "#0000ff" >        <  View      android:layout_width= "match_parent    "       android:layout_height= "Match_parent"      android:background= "#ff0000" />    <  View      android:layout_width= "match_parent    "       android:layout_height= "Match_parent"      android:background= "#00ff00" />      </com.example.layout.MyViewGroup></relativelayout>
Effect:

Myviewgroup is blue, and two sub-view are red and green, respectively.

In Custom view, OnLayout is used in conjunction with the Onmeasure method to implement a complex layout for custom view. The custom view first calls Onmeasure to measure, then calls the OnLayout method, dynamically acquires the measured size of the child view and child view, and then layouts the layout.

Android's OnLayout, layout method explained

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.