In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to use ViewGroup custom layout in Android". In daily operation, I believe many people have doubts about how to use ViewGroup custom layout in Android. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use ViewGroup custom layout in Android". Next, please follow the editor to study!
Steps
Here I will design a ViewGroup similar to LinearLayout linear layout as an example.
First of all, if it is a LinearLayout, then when setting wrap_content, it will take the widest subspace as its width. At the same time, it will be the sum of the height of all child controls in terms of height. So we first write two methods to measure the width and height of the ViewGroup.
Private int getMaxWidth () {int count = getChildCount (); int maxWidth = 0; for (int I = 0; I < count; iTunes +) {int currentWidth = getChildAt (I). GetMeasuredWidth (); if (maxWidth < currentWidth) {maxWidth = currentWidth;}} return maxWidth;} private int getTotalHeight () {int count = getChildCount (); int totalHeight = 0; for (int I = 0; I < count; iTunes +) {totalHeight + = getChildAt (I). GetMeasuredHeight ();} return totalHeight;}
For ViewGroup, we can roughly divide it into two modes: fixed length and width mode (match_parent) and adaptive mode (wrap_content). According to these two modes, we can divide the rendering of ViewGroup. With regard to the measureChildren method here, it is used to measure all sub-View, which triggers the onMeasure function of each sub-View, but we should pay attention to distinguish it from measureChild. MeasureChild measures a single view.
Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure (widthMeasureSpec, heightMeasureSpec); measureChildren (widthMeasureSpec, heightMeasureSpec); int widthMode = MeasureSpec.getMode (widthMeasureSpec); int width = MeasureSpec.getSize (widthMeasureSpec); int heightMode= MeasureSpec.getMode (heightMeasureSpec); int height = MeasureSpec.getSize (heightMeasureSpec); if (widthMode = = MeasureSpec.AT_MOST & & heightMode= = MeasureSpec.AT_MOST) {int groupWidth = getMaxWidth (); int groupHeight= getTotalHeight (); setMeasuredDimension (groupWidth, groupHeight) } else if (widthMode = = MeasureSpec.AT_MOST) {setMeasuredDimension (getMaxWidth (), height);} else if (heightMode = = MeasureSpec.AT_MOST) {setMeasuredDimension (width, getTotalHeight ());}}
Rewrite onLayout
After finishing the above things, our layout size 79 has come out, but let's add it to the active layout file, add a few sub-View, and then run it to see the effect:
The running effect is as follows:
We saw that the layout came out, and the size seemed to be all right, but what about the sub-View?! I don't see the child View looking at the code, the onLayout () we rewrote before the system is still empty! In other words, the size and location of the sub-View have not been set at all! Let's rewrite the onLayout () method.
@ Override protected void onLayout (boolean changed, int l, int t, int r, int b) {int count = getChildCount (); int currentHeight = 0; for (int I = 0; I < count; iTunes +) {View view = getChildAt (I); int height = view.getMeasuredHeight (); int width = view.getMeasuredWidth (); view.layout (l, currentHeight, l + width, currentHeight + height); currentHeight + = height }} at this point, the study on "how to use ViewGroup custom layout in Android" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.