Custom ViewGroup Control (ii)-----> Flow Layout Advanced (ii)

Source: Internet
Author: User


Main.xml

<?xml version= "1.0" encoding= "Utf-8"? ><com.example.simplelayout.mylinlayout xmlns:android= "/http Schemas.android.com/apk/res/android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_ The parent "android:layout_height=" wrap_content "android:background=" #ff00ff "tools:context=".        Mainactivity "><!--add layout_margin parameters to the XML--<textview android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:layout_margintop= "10DP" android:background= "#ff0000" an droid:text= "First View"/> <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_c    Ontent "android:layout_margintop=" 20DP "android:background=" #00ff00 "android:text=" Second View "/> <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_ margintop= "30DP" android:background= "#0000ff" AndroiD:text= "A third view"/></com.example.simplelayout.mylinlayout> 

Mainactivity

Package Com.example.simplelayout;import Android.app.activity;import Android.os.bundle;public class MainActivity Extends Activity {    @Override public    void OnCreate (Bundle savedinstancestate) {        super.oncreate ( Savedinstancestate);        Setcontentview (R.layout.main);    }}

Mylinlayout

Package Com.example.simplelayout;import Android.content.context;import Android.util.attributeset;import Android.view.view;import android.view.viewgroup;/** */** onmeasure (): Measure your size, your size, and provide recommendations for a formal layout. (note, just suggest, as to use not, to see OnLayout); * OnLayout (): Use the layout () function to lay out all child controls; OnDraw (): Draw according to the location of the layout; * */public class Mylinlayout extends ViewGroup {/** * constructor--apart, straight Then write out three to * * @param context */public mylinlayout (context) {super ';} Public Mylinlayout (context context, AttributeSet Attrs) {Super (context, attrs);} Public Mylinlayout (context context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr);} /** * If you want to customize ViewGroup supports Layout_margin parameters for child controls, the custom ViewGroup class must overload the Generatelayoutparams * () function, and returns a Viewgroup.marginlayoutparams derived class object in the function in order to use the margin parameter. */@Overrideprotected Layoutparams generatelayoutparams (Layoutparams p) {return new Marginlayoutparams (p);} /** * Gets the corresponding layout_width and layout_height values from the specified XML */// If we still need margin-related parameters, we can only rewrite the Generatelayoutparams () function: @Overridepublic LAyoutparams generatelayoutparams (AttributeSet attrs) {return new Marginlayoutparams (GetContext (), attrs);} /** * Generatedefaultlayoutparams () function. Returns an instance of the corresponding marginlayoutparams () directly *//** * generates Layout_width= "Wrap_content", layout_height= "wrap_content" If you want to use the default constructor method * The corresponding parameter *//** * Why the Generatelayoutparams () function must be overridden, because the default generatelayoutparams () * function extracts only the values of Layout_width *, Layout_height, Only Marginlayoutparams () has the ability to extract margin spacing!!!! */@Overrideprotected Layoutparams generatedefaultlayoutparams () {return new Marginlayoutparams (LAYOUTPARAMS.WRAP_ content,layoutparams.wrap_content);} /** * This viewgroup's wide-height attribute android:layout_width= "match_parent"--exactly (OK) * android:layout_height= "wrap_content"--AT_ Most (indeterminate) * * They are the parent class passed over to the current view of a suggested value, the recommended value, that is, to set the current view size to wide widthmeasurespec, * High Heightmeasurespec * *②, exactly (full), The parent element determines the exact size of the element, and the child element is scoped to the given boundary and ignores its size, *③, At_most (at most), and the child element reaches a value of at most the specified size. */@Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (Widthmeasurespec, Heightmeasurespec);//width, height int measurewidth = measurespec.getsize (widthmeasurespec); int measureheight = Measurespec.getsize ( HEIGHTMEASURESPEC);//measurement mode int measurewidthmode = Measurespec.getmode (widthmeasurespec); int measureheightmode = Measurespec.getmode (HEIGHTMEASURESPEC);//Initialize viewgroup wide, high int viewgroupheight = 0;int Viewgroupwidth = 0;// Get each child in ViewGroup view, traverse int count = Getchildcount (); for (int i = 0; i < count; i++) {//Get each child view object in turn view children = ge Tchildat (i);//measure each child's view, pass the pattern of the parent class into it------------------------click on the source Measurechild Get marginlayoutparams Layout Parameters!!!!!!!!!!!!!!!!!!!!!!! /** * Because the return value of Generatelayoutparams () is a layoutparams instance, * and Marginlayoutparams is derived from Layoutparam *; so depending on the polymorphic nature of the class, You can directly convert the Layoutparams instance at this time directly to marginlayoutparams instance; * So the following sentence here is not error: */marginlayoutparams LP = (marginlayoutparams) Child.getlayoutparams (); int childheight = Child.getmeasuredheight () + lp.topmargin+ lp.bottommargin;int childWidth = Child.getmeasuredwidth () + lp.leftmargin+ lp.rightmargin;//ViewgrouP height Increment viewgroupheight + = childheight;//viewgroup width take maximum viewgroupwidth = Math.max (Childwidth, viewgroupwidth);} The width of the viewgroup does not need to be measured directly "match_parent"--exactly//High is "wrap_content"--at_most, need to accumulate to get the height/** *②, exactly (full), the parent element determines the exact size of the element , the child element is limited to a given boundary and ignores its size, *③, At_most (at most), and the child element reaches a value of at most the specified size. */setmeasureddimension (Measurewidthmode = = measurespec.exactly)? measurewidth:viewgroupwidth, (MeasureHeightMode = = measurespec.exactly)? Measureheight:viewgroupheight);}  @Overrideprotected void OnLayout (Boolean changed, int l, int t, int r, int b) {int top = 0;int count = Getchildcount (); for (int i = 0; i < count; i++) {View child = Getchildat (i);//Get marginlayoutparams layout parameters!!!!!!!!!!!!!!!!!!!! Marginlayoutparams LP = (marginlayoutparams) child.getlayoutparams (); int childheight = Child.getmeasuredheight () + lp.topmargin+ Lp.bottommargin;int childwidth = child.getmeasuredwidth () + lp.leftmargin+ lp.rightMargin;child.layout ( 0, top, childwidth, top + childheight); top + + Childheight;}}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Custom ViewGroup Control (ii)-----> Flow Layout Advanced (ii)

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.