Viewgroup is simply a view that can be installed. today, I encountered a problem: I needed a layout that could automatically wrap the line according to the width of the view in a row. I found it online. There are no relevant examples, but I found the idea: define a viewgroup, and then automatically check the abscissa value of the right edge of the view in the onlayout file, and determine whether to display the view with line breaks based on the state of the parent view of your view. Because the code is relatively simple, I will not say much about it:
1 public class MyViewGroup extends ViewGroup {
2 private final static String TAG = "MyViewGroup";
3
4 private final static int VIEW_MARGIN=2;
5
6 public MyViewGroup(Context context) {
7 super(context);
8 }
9 @Override
10 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
11 Log.d(TAG, "widthMeasureSpec = "+widthMeasureSpec+" heightMeasureSpec"+heightMeasureSpec);
12
13 for (int index = 0; index < getChildCount(); index++) {
14 final View child = getChildAt(index);
15 // measure
16 child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
17 }
18
19 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
20 }
21
22 @Override
23 protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
24 Log.d(TAG, "changed = "+arg0+" left = "+arg1+" top = "+arg2+" right = "+arg3+" botom = "+arg4);
25 final int count = getChildCount();
26 int row=0;// which row lay you view relative to parent
27 int lengthX=arg1; // right position of child relative to parent
28 int lengthY=arg2; // bottom position of child relative to parent
29 for(int i=0;i<count;i++){
30
31 final View child = this.getChildAt(i);
32 int width = child.getMeasuredWidth();
33 int height = child.getMeasuredHeight();
34 lengthX+=width+VIEW_MARGIN;
35 lengthY=row*(height+VIEW_MARGIN)+VIEW_MARGIN+height+arg2;
36 //if it can't drawing on a same line , skip to next line
37 if(lengthX>arg3){
38 lengthX=width+VIEW_MARGIN+arg1;
39 row++;
40 lengthY=row*(height+VIEW_MARGIN)+VIEW_MARGIN+height+arg2;
41
42 }
43
44 child.layout(lengthX-width, lengthY-height, lengthX, lengthY);
45 }
46
47 }
48
49 }
Note that the Drawing Process of viewgroup is as follows: Drawing of viewgroup involves two steps: 1. Measure 2. layout.
Call the callback function in two steps: 1. onmeasure () 2. onlayout ()
1. onmeasure () in this function, viewgroup will accept the size of the childview request, and store it in childview through the measure (newwidthmeasurespec, heightmeasurespec) function of childview, so that getmeasuredwidth () of childview () the value of andgetmeasuredheight () can be obtained by subsequent work.
2. onlayout () in this function, viewgroup will get getmeasuredwidth () andgetmeasuredheight () of childview to layout all childview.
3. View. measurespec
And layoutparams
These two classes are used by viewgroup to negotiate the size with childview. View. measurespec is used to deploy viewgroup.
Layoutparams is used by childview to tell the viewgroup what I need.
4. At the end of onmeasure of the view, call setmeasureddimension () to store the size of the view. This method determines the size of the current view.
: