Introduction to the first view in the Android group English notes series
After learning the first line of code, I continue to learn about the English series of Android games. I hope I can stick to it, learn this book well, and take relevant notes in time to consolidate relevant knowledge;
1. View introduction:
1 '. Let's first look at the view tree structure:
2. Tree content:
A. viewparent is the core of the entire tree. It schedules and distributes all interactive management events in a unified manner.
B. Controls are divided into viewgroup and view controls. As a parent control, viewgroup can contain multiple vie w controls.
C. We know that the findViewById method must be used to instantiate the control before using the control. In the tree structure, the function is to find the corresponding elements through the depth-first traversal of the tree.
3. UI architecture diagram:
4. UI architecture diagram content:
A. In fact, each activity contains a window object, which is implemented through PhoneWindow.
B. PhoneWindow sets another DecorView as the root View of the entire application window. (Simply put, the entire cell phone screen we see is the entire DecorView );
C. in our mobile app, there are usually two views, one is the title bar, that is, TitleView, and the other is the view that presents the content, that is, ContentView. (Of course, we can also hide it in some ways, for example, before setContentView:
RequestWindowFeature (Window. FEATURE_NO_TITLE) method .)
D. display process of the interface: when the program calls the setContentView method in the onCreate () method, ActivityManagerService calls back the onResume method and the system adds the entire DecorView to PhoneWindow.
2. View measurement:
1. There are three measurement modes:
EXACTLY mode: it refers to the exact value mode. For example, if we set layout_width = 30dp, this mode belongs to the exact value mode.
AT_MOST mode: it refers to the maximum value mode. For example, the wrap_content mode we use belongs to this mode, which allows changes with the content, as long as it does not exceed the maximum size allowed by the parent control.
UNSPECIFIED mode: there is no limit on the mode. The mode can be as big as it is needed. It is generally used in custom views.
2. About rewriting the onMeasure () method:
A. Why rewrite the onMeasure method?
Because the default onMeasure method of the view class only supports the EXACTLY mode, if we want to specify the size of wrap_content, We must override this method.
B. Rewrite the onMeasure process:
(1) create a class to inherit to view:
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec);}
We will see such an initial onMeasure function, and then let's look at the super. OnMeasure method:
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));}
So here we want to rewrite the setMeasuredDimension function: let's add the following sentence:
setMeasuredDimension(measureSize(200,widthMeasureSpec),measureSize(200,heightMeasureSpec));
Let's take a look at the meaning of this sentence. In fact, we have defined a measureSize function, and then passed in two parameters respectively. The first is the default value and the second is the actual value. Then let's take a look at how the UDF is implemented:
Private int measureSize (int defaultSize, int measureSpec) {int result = 0; // defines result as the return value int specMode = MeasureSpec. getMode (measureSpec); // get the Measurement Mode int specSize = MeasureSpec. getSize (measureSpec); // obtain the measured value if (specMode = MeasureSpec. EXACTLY) {// if it is an exact measurement, return the measured value result = specSize;} else {result = defaultSize; // if it is in another two modes, set to the default value if (specMode = MeasureSpec. AT_MOST) {// If the mode is the maximum value, we need to obtain the minimum value of the default value and the measured value result = Math. min (result, specSize) ;}} return result ;}
Finally, we reference this custom view in the xml file:
<main.view.com.view.viewTest android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ff0000" />