Android Custom ViewGroup First Contact viewgroup_android

Source: Internet
Author: User

Collation Summary from Hongyang Blog: http://blog.csdn.net/lmj623565791/article/details/38339817/
First, Com.cctvjiatao.customviewgroup.act.MainActivity.Java
Requirements: We define a viewgroup, the interior can be passed 0 to 4 Childview, respectively, shown in the upper left corner, the upper right corner, lower left corner, lower right corner

public class Mainactivity extends Appcompatactivity {

 @Override
 protected void OnCreate (Bundle Savedinstancestate) {
  super.oncreate (savedinstancestate);
  Setcontentview (r.layout.activity_main);  Setcontentview (r.layout.activity_main2);  Setcontentview (r.layout.activity_main3);
 }


Second, Com.cctvjiatao.customviewgroup.view.CustomViewGroup.java
a), ViewGroup is what? Role?
1, it is equivalent to placing the view container. In an XML layout file, attributes that start with "layout" are related to ViewGroup (i.e., the container), such as height (layout_height), Width (layout_width), Alignment (layout_gravity), and so on;
2, it gives childview to calculate the recommended width, height and measurement mode, determine the position of childview;
Why is "recommended width, height" rather than direct certainty? Because when the width and height of the Childview is set to Wrap_content, only Childview can calculate its width and height.
Second, what is the role of view?
1, it according to the measurement mode and viewgroup given the recommendations of the wide, high, calculate their own wide, high;
2, in ViewGroup for its designated area to draw their own form;
( III) Three kinds of measurement modes of view
1, exactly: set a precise value, generally when Childview set its width high for the exact value, Match_parent, ViewGroup will set it to exactly;
2, At_most: that the child layout is limited to a maximum value, generally when the Childview set its width, height for wrap_content, ViewGroup will be set to At_most;
3, UNSPECIFIED: said the child layout wants how big, generally appears in the Aadapterview item Heightmode, ScrollView Childview in the heightmode; this kind of pattern is relatively rare.
Iv. The relationship between ViewGroup and Layoutparams
When writing Childview in LinearLayout, you can write layout_gravity,layout_weight attributes;
But in the Relativelayout Childview has the layout_centerinparent attribute, but does not have layout_gravity,layout_weight, this is why?
This is because each viewgroup needs to specify a layoutparams to determine which properties are supported Childview support, such as LinearLayout designated Linearlayout.layoutparams, etc.
If you go to see LinearLayout's source code, you will find that its internal definition of linearlayout.layoutparams, in this category, you can find weight and gravity figure.
V. Analyzing the role of viewgroup and view from an API perspective
View determines its width and height (completed in onmeasure) according to the measured values and measurement patterns passed in by ViewGroup, and then completes the drawing of itself in OnDraw;
ViewGroup need to give the view the measurement and measurement mode (completed in onmeasure), and for this viewgroup parent layout, ViewGroup also in onmeasure to complete their own wide, high determination;
ViewGroup needs to be onlayout to complete the designation of its childview position.

public class Customviewgroup extends ViewGroup {public Customviewgroup (context) {super);
 Public Customviewgroup (context, AttributeSet attrs) {Super (context, attrs); 
 Public Customviewgroup (context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr); /** * I, rewrite the generatelayoutparams, and determine that the layoutparams of the ViewGroup returns an instance of Marginlayoutparams, which specifies its viewgroup for our Layoutpar AMS for Marginlayoutparams */@Override public layoutparams generatelayoutparams (AttributeSet attrs) {return new Margin
 Layoutparams (GetContext (), attrs); /** * II, calculate the width and height of all childview and then set its own width and height/@Override protected void onmeasure (int widthmeasuresp) According to the Childview calculation result EC, int heightmeasurespec) {//1, obtain this ViewGroup superior container for its recommended width and height, and compute mode int widthmode = Measurespec.getmode (Widthmeasurespec)
  ;
  int heightmode = Measurespec.getmode (Heightmeasurespec);
  int sizewidth = measurespec.getsize (Widthmeasurespec); int sizeheight = Measurespec.GetSize (HEIGHTMEASURESPEC);
  2, calculate all the Childview width and height measurechildren (widthmeasurespec, Heightmeasurespec);
  3, if the ViewGroup layout is wrap_content, according to the Childview size, compute the width of the container wide and high int width = 0;//viewgroup the height of int height = 0;//viewgroup int ccount = Getchildcount (); number of//childview int cwidth = 0;//childview total width int cheight = total height of 0;//childview marginlay Outparams cparams = null;//view measurement mode int lheight = 0;//the height int childview = Rheight used to compute the left two 0;//is used to compute the height of the two childview on the right, the most The final height takes the large value int twidth = 0;//is used to compute the width int bwidth of the top two Childview = 0;//is used to calculate the width of the following two Childiew, and the final width takes a large value for (int i = 0; i < ccount;
   i++) {View Childview = Getchildat (i);
   Cwidth = Childview.getmeasuredwidth ();
   Cheight = Childview.getmeasuredheight ();
   Cparams = (marginlayoutparams) childview.getlayoutparams ();
   if (i = = 0 | | | i = = 1) {//above two childview twidth + = cwidth + Cparams.leftmargin + cparams.rightmargin; } if (i = = 2 | | | i = = 3) {//below two Childview bwidth + = Cwidth + Cparams.leftmaRgin + cparams.rightmargin;
   } if (i = = 0 | | i = = 2) {//left two Childview lheight + = cheight + Cparams.topmargin + cparams.bottommargin;
   } if (i = = 1 | | i = = 3) {//Right two Childview rheight + = cheight + Cparams.topmargin + cparams.bottommargin; } width = Math.max (twidth, bwidth);//Max width height = math.max (lheight, rheight);//GO max height//4, if the wrap_content is set to our account Otherwise set directly to the value computed by the parent container setmeasureddimension (Widthmode = = measurespec.exactly)? sizewidth:width, (Heightmode = = measurespec.exactly)?
 Sizeheight:height);  /** * III, rewrite onlayout, locate all of its childview (set Childview area)/@Override protected void OnLayout (Boolean changed, int
  l, int t, int r, int b) {int ccount = Getchildcount ();
  int cwidth = 0;
  int cheight = 0;
  Marginlayoutparams cparams = null;
   Traverse all Childview According to its width and height, and margin for (int i = 0; i < ccount; i++) {View Childview = Getchildat (i);
   Cwidth = Childview.getmeasuredwidth (); Cheight = Childview.getmeasuredheiGht ();
   Cparams = (marginlayoutparams) childview.getlayoutparams ();
   int cl = 0, CT = 0, CR = 0, cb = 0;
     switch (i) {case 0:cl = Cparams.leftmargin;
     ct = cparams.topmargin;
    Break
     Case 1:CL = getwidth ()-cwidth-cparams.leftmargin-cparams.rightmargin;
     ct = cparams.topmargin;
    Break
     Case 2:CL = Cparams.leftmargin;
     ct = getheight ()-cheight-cparams.bottommargin;
    Break
     Case 3:CL = getwidth ()-cwidth-cparams.leftmargin-cparams.rightmargin;
     ct = getheight ()-cheight-cparams.bottommargin;
   Break
   CR = cl + cwidth;
   CB = cheight + ct;
  Childview.layout (CL, CT, CR, CB);

 }
 }
}

three or three kinds of layouts
 activity_main.xml

<com.cctvjiatao.customviewgroup.view.customviewgroup xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" 200DP "android:layout_height=" 200DP "Android:background=" #AA333333 > <textview android:layout_width= "50dp" android:layout_height= "50DP" Androi D:background= "#FF4444" android:gravity= "center" android:text= "0" android:textcolor= "#FFFFFF" android:textsize= "22s P "android:textstyle=" bold "/> <textview android:layout_width=" 50DP "android:layout_height=" 50DP "Android: 
  Background= "#00ff00" android:gravity= "center" android:text= "1" android:textcolor= "#FFFFFF" android:textsize= "22SP" android:textstyle= "Bold"/> <textview android:layout_width= "50DP" android:layout_height= "50DP" Android:ba
  Ckground= "#ff0000" android:gravity= "center" android:text= "2" android:textcolor= "#FFFFFF" android:textsize= "22SP" android:textstyle= "Bold"/> <textvieW android:layout_width= "50DP" android:layout_height= "50DP" android:background= "#0000ff" android:gravity= "center" android:text= "3" android:textcolor= "#FFFFFF" android:textsize= "22SP" android:textstyle= "bold"/> </com.cctvj

 Iatao.customviewgroup.view.customviewgroup>

Activity_main2.xml

<com.cctvjiatao.customviewgroup.view.customviewgroup xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Wrap_content "android:layout_height = "Wrap_content" android:background= "#AA333333" > <textview android:layout_width= "150DP" android:layout_height = "150DP" android:background= "#E5ED05" android:gravity= "center" android:text= "0" android:textcolor= "#FFFFFF" Andr Oid:textsize= "22SP" android:textstyle= "bold"/> <textview android:layout_width= "50DP" android:layout_height= "50DP" android:background= "#00ff00" android:gravity= "center" android:text= "1" android:textcolor= "#FFFFFF" Androi D:textsize= "22SP" android:textstyle= "bold"/> <textview android:layout_width= "50DP" android:layout_height= "5 0DP "android:background=" #ff0000 "android:gravity=" center "android:text=" 2 "android:textcolor=" #FFFFFF "Android: Textsize= "22SP" android:textstyle= "bold"/&GT <textview android:layout_width= "50DP" android:layout_height= "50DP" android:background= "#0000ff" android:gravity = "Center" android:text= "3" android:textcolor= "#FFFFFF" android:textsize= "22SP" android:textstyle= "bold"/>

 ;/com.cctvjiatao.customviewgroup.view.customviewgroup>

Activity_main3.xml

<com.cctvjiatao.customviewgroup.view.customviewgroup 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:background= "#AA333333" > <textview android:layout_width= "150DP" android:layout_height = "150DP" android:background= "#E5ED05" android:gravity= "center" android:text= "0" android:textcolor= "#FFFFFF" Andr Oid:textsize= "22SP" android:textstyle= "bold"/> <textview android:layout_width= "50DP" android:layout_height= "50DP" android:background= "#00ff00" android:gravity= "center" android:text= "1" android:textcolor= "#FFFFFF" Androi D:textsize= "22SP" android:textstyle= "bold"/> <textview android:layout_width= "50DP" android:layout_height= "5 0DP "android:background=" #ff0000 "android:gravity=" center "android:text=" 2 "android:textcolor=" #FFFFFF "Android: Textsize= "22SP" android:textstyle= "bold"/&GT <textview android:layout_width= "150DP" android:layout_height= "150DP" android:background= "#0000ff" Android:gravi ty= "Center" android:text= "3" android:textcolor= "#FFFFFF" android:textsize= "22SP" android:textstyle= "bold"/> &

 Lt;/com.cctvjiatao.customviewgroup.view.customviewgroup>

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.