Android's OnLayout, layout method explained

Source: Internet
Author: User

Http://www.2cto.com/kf/201404/291740.html

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:

?
123 @Override protected Abstract void onlayout ( boolean changed,               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. View placement is placed according to a rectangular space, onlayout pass down l,t,r,b is placed in the parent control of the rectangle free space (minus the margin and padding space) of the left, top and bottom right, and bottom values.


Layout method:

?
1 publicvoid layout(int l, int t, int r, intb);

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. For example, if incoming (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

?
12345678910111213141516171819202122232425 public class MyViewGroup extends ViewGroup {    // 子View的水平间隔    private final static int padding = 20;        public MyViewGroup(Context context, AttributeSet attrs) {        super(context, attrs);        // TODO Auto-generated constructor stub    }    @Override    protected void onLayout(boolean changed, int l, int t, int r, int b) {        // TODO Auto-generated method stub                // 动态获取子View实例        for (int i = 0, size = getChildCount(); i < size; i++) {            View view = getChildAt(i);            // 放置子View,宽高都是100            view.layout(l, t, l + 100, t + 100);            l += 100 + padding;        }            }    }


XML layout of activity:

?
12345678910 <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">            </view></view></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.