So I think I may have figured this out, though I'd like to see Momo or
someone else confirm this.
The problem seems to have been that in my custom layout that extends
ViewGroup I did override onLayout but not onMeasure. The solution seems to
be to override onMeasure this way:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
// At this time we need to call setMeasuredDimensions(). Lets just call
the
// parent View's method (see
https://github.com/android/platform_frameworks_base/blob/master/core/java/android/view/View.java
)
// that does:
// setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(),
widthMeasureSpec),
// getDefaultSize(getSuggestedMinimumHeight(),
heightMeasureSpec));
//
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int wspec =
MeasureSpec.makeMeasureSpec(getMeasuredWidth()/getChildCount(), MeasureSpec.
EXACTLY);
int hspec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.
EXACTLY);
for(int i=0; i<getChildCount(); i++){
View v = getChildAt(i);
Log.d(TAG, "Measured Width / Height: "+getMeasuredWidth()+", "
+getMeasuredHeight());
v.measure(wspec, hspec);
}
}
I have an example of a custom layout here:
https://github.com/arpit/CustomLayoutExample
Hope this helps.
On Tue, Jun 26, 2012 at 6:57 PM, Arpit Mathur <[email protected]>wrote:
> I am seeing the exact same thing. Does anyone have an answer to this?
>
>
> On Friday, March 2, 2012 7:39:16 PM UTC-5, momo wrote:
>>
>> 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 [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en