[android-developers] Custom layout - why is behavior different when laying out a ViewGroup (vs View) as a child
I've got a custom layout for positioning tooltips on a pan-n-zoom tiled map (also custom), in reaction to touch events. The tooltips should appear above and centered to the marker that fired it.
Everything works perfectly when using normal Views (ImageView, TextView) as children. However, when trying to use a ViewGroup (e.g., a RelativeLayout), the child does not appear at all.
I added some logging to onLayout, and it was reporting 0 for child.getMeasuredWidth()/Height(), so I extended RelativeLayout and overrode onMeasure in order to supply the correct dimensions. The dimensions are now logging correctly, but still the child ViewGroup does not appear. This doesn't seem like it's a necessary step in any case - I'd expect to be able to use layouts as children normally.
Why is there a difference? Why would a simple View appear and position exactly as expected, but child layouts fail to render at all?
Here's a summarized version of the custom layout:
public class TooltipLayout extends ViewGroup {
public TooltipLayout(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//...
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
if (child.getVisibility() != GONE) {
TooltipLayout.LayoutParams lp = (TooltipLayout.LayoutParams) child.getLayoutParams();
child.layout(lp.x, lp.y, lp.x + child.getMeasuredWidth(), lp.y + child.getMeasuredHeight());
}
}
}
public static class LayoutParams extends ViewGroup.LayoutParams {
public int x;
public int y;
public LayoutParams(int width, int height, int left, int top) {
super(width, height);
x = left;
y = top;
}
}
}
TYIA.
-- You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home