简体   繁体   中英

Android custom layout manager - layout in children

I have created a custom layout manager derived from LinearLayout. (I do not really use much of LinearLayout functionality but I do not want to care about all of many ViewGroup abstract methods.) I have overwritten only

protected void onLayout(boolean changed, int l, int t, int r, int b)

method and I call child.layout(...) in MyLayout.onLayout(...) for each of its children. All other stuff is original LinearLayout. It will work fine and children will be placed correctly in their parent MyLayout if the children are simple controlls without children (grandchildren of the MyLayout).

Problems come when I use some ViewGroup (LinearLayout, ViewSwitcher, ...) with children as child of MyLayout. In this case children (of MyLayout) are placed well but grandchildren (of MyLayout) inside of each child not. It seems paramaters and values like wrap_content, fill_parent, layoutWeight are not handled correctly. For example ViewSwitcher in MyLayout does not show its content, LinearLayouts (child of MyLayout) children (grandchildren of MyLayout) with fill_parent have size of 0px, CompoundButton (child of MyLayout) sometimes does not show its text...

It seems child.layout(...) in MyLayout.onLayout sets the size and position of each child correctly but I need to call some other method to make layout in each child working well (if the child is ViewGroup with children). Is there something like that or MyLayout with just changed onLayout should work well?

Thanks for all advices.

sounds like you need to overide onMeasure and call measure() on each of the children. if you children are viewgroups and the grandchildren are views then you want to loop through the children and call measure(int,int). then the children should call measure on the grandchildren.

take a look at the linearLayout source: http://www.grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3.6_r1/android/widget/LinearLayout.java#LinearLayout.onMeasure%28int%2Cint%29

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM