If you change the size of a child, you need to measure it with that size first.

On Sun, Sep 26, 2010 at 7:38 AM, John Gaby <jg...@gabysoft.com> wrote:
> I have a custom ViewGroup.  The idea is to be able to place controls
> at absolute positions and then have the ViewGroup scale those controls
> based on the difference in the screen dimensions with respect to the
> original  design dimensions.  I am using the code below.  In response
> to the 'onMeasure' message, I am storing the width and height of the
> view.  Then on the 'onLayout' message I am computing new dimensions
> for the child controls and adjusting their sizes proportionally.  This
> all works to some degree.  However for certain controls (e.g. button
> controls), the control resizes, but the text inside does not remain
> centered within the control.  I am thinking that there is some other
> call I need to make to update the child controls after I change their
> size.
>
> Any suggestions?
>
> Thanks
>
> @Override
> protected void onMeasure(int wid, int hgt)
> {
>    super.onMeasure(wid, hgt);
>
>    m_viewWidth  = getMeasuredWidth();
>    m_viewHeight = getMeasuredHeight();
>
>    measureChildren(wid, hgt);
> }
>
> @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)
>        {
>            GViewGroup.LayoutParams lp = (GViewGroup.LayoutParams)
> child.getLayoutParams();
>
>            int wid = child.getMeasuredWidth();
>            int hgt = child.getMeasuredHeight();
>            int lx = lp.x;
>            int ly = lp.y;
>            int ux = lp.x + wid;
>            int uy = lp.y + hgt;
>
>            if ((m_designWidth != 0) && (m_designHeight != 0) &&
> (m_viewWidth != 0) && (m_viewHeight != 0))
>            {
>             lx = (lx * m_viewWidth) / m_designWidth;
>             ux = (ux * m_viewWidth) / m_designWidth;
>             ly = (ly * m_viewHeight) / m_designHeight;
>             uy = (uy * m_viewHeight) / m_designHeight;
>            }
>
>            child.layout(lx, ly, ux, uy);
>        }
>    }
> }
>
> --
> 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+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>



-- 
Romain Guy
Android framework engineer
romain...@android.com

Note: please don't send private questions to me, as I don't have time
to provide private support.  All such questions should be posted on
public forums, where I and others can see and answer them

-- 
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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to