Custom ViewGroup controls (i)-----> Flow Layout Advanced (i)

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 "> <textview android:layout_width=" wrap_content "android:layout_height=" Wrap_content " android:layout_margintop= "10DP" android:background= "#ff0000" android:text= "First View"/> <text View android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_marginto p= "20DP" android:background= "#00ff00" android:text= "Second View"/> <textview android:layout_wid Th= "Wrap_content" android:layout_height= "wrap_content" android:layout_margintop= "30DP" android:backgr Ound= "#0000ff" android:text= "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 the 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 {/** * First is 3 constructors */p Ublic Mylinlayout (Context context) {super (context);} Public Mylinlayout (context context, AttributeSet Attrs) {Super (context, attrs);} Public Mylinlayout (context context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr);} /** * 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;//gets each child in the ViewGroup view, traversing int count = Getchildcount (), for (int i = 0; i < count; i++) {///Get every Child Sub-View Object View child = Getchildat (i);//measure each child's view and pass the pattern of the parent class into it--point to source Measurechild (child, Widthmeasurespec, HEIGHTMEASURESPEC); int childheight = Child.getmeasuredheight (); int childwidth = Child.getmeasuredwidth ();// 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);} The/** * Getmeasurewidth () method is available at the end of the measure () process, and the GetWidth () method is not available until the layout () * process is finished. Once again, */@Overrideprotected void OnLayout (Boolean changed, int l, int t, int r, int b) {int top = 0;//Gets the number of child view int count = Getchildcount (); for (int i = 0; i < count; i++) {//Get each child's view object in order, view children = Getchildat (i);//Get the child view's wide-high int CHILDH eight = Child.getmeasuredheight (); int childwidth = Child.getmeasuredwidth (); child.layout (0, Top, childwidth, Top + Childheight);//increment top value of child view top + = Childheight;}}}


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

Custom ViewGroup controls (i)-----> Flow Layout Advanced (i)

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.