First contact with ViewGroup for Android custom ViewGroup

Summarize from Hongyang's blog: http://blog.csdn.net/lmj623565791/article/details/38339817/ 1、 Com.cctv jiatao.customviewgroup.act.mainactivity.java requirements: we define a ViewGroup. 0 to 4 childviews can be passed in, which are displayed in the upper left corner, upper right corner, lower left corner and lower right corner respectively

2、 Com.cctv jiatao.customviewgroup.view.customviewgroup.java I) what is ViewGroup? What's the effect? 1. It is equivalent to the container where the view is placed. In the XML layout file, all attributes starting with "layout" are related to ViewGroup (i.e. container), such as layout_height, layout_width, layout_gravity, etc; 2. It calculates the recommended width, height and measurement mode for childview and determines the position of childview; Why is it "recommended width and height" rather than direct determination? Because when the width and height of childview are set to wrap_ Content, only childview can calculate its own width and height. 2) What is the function of view? 1. It calculates its own width and height according to the measurement mode and the recommended width and height given by ViewGroup; 2. Draw your own shape in the area specified by ViewGroup; 3) Three measurement modes of childview 1. Actual: indicates that an accurate value is set. Generally, when childview sets its width and height to an accurate value and match_ When parent, ViewGroup will set it to actual; 2、AT_ Most: indicates that the sub layout is limited to a maximum value. Generally, when childview sets its width and height to wrap_ Content, ViewGroup will set it to at_ MOST; 3. Unspecified: indicates that the sub layout can be as large as it wants. It usually appears in the highmode of item in aadapterview and highmode of childview in Scrollview; This model is rare. 4) , ViewGroup and layoutparams. When childview is written in LinearLayout, layout can be written_ gravity,layout_ Weight attribute; The childview in relativelayout has layout_ Centerinparent property without layout_ gravity,layout_ Weight, why? This is because each ViewGroup needs to specify a layoutparams to determine which properties support childview. For example, LinearLayout specifies linearlayout.layoutparams. If you look at the source code of LinearLayout, you will find that linearlayout.layoutparams is defined internally. In this class, you can find the shadow of weight and gravity. 5) Analyze the functions of ViewGroup and view from the perspective of API. View determines its own width and height (completed in onmeasure) according to the measurement values and measurement modes passed in by ViewGroup, and then completes its own drawing in OnDraw; The ViewGroup needs to transfer the measurement value and measurement mode of the view to the view (completed in onmeasure), and for the parent layout of this ViewGroup, the ViewGroup also needs to determine its own width and height in onmeasure; ViewGroup needs to specify the location of its childview in onlayout.

3、 Three layout activities_ main.xml

activity_ main2.xml

activity_ main3.xml

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>