Brill,

Views specify their preferred size by overriding onMeasure.

You've got to deal with special values representing wrap_content and fill_parent (that's the MeasureSpec stuff):

Here is an example, it's a view that's placed inside a ScrollView and makes itself "tall", even in landscape orientation:

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (heightMeasureSpec != 0) {
            int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
            int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);

            if (sizeHeight < sizeWidth) {
                sizeHeight = sizeWidth * 3 / 2;

                heightMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec
                        .getMode(heightMeasureSpec), sizeHeight);
            }
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

-- Kostya

24.01.2011 7:45, Brill Pappin ?????:
Ok, I'm working on a custom view and having trouble with setting its size so it scales properly across devices.

In particular i want its fixed height to be relative to its width (of match_parent).

Something like this sudo code:

View myview = new View(..);

myview.setLayoutParams(new LayoutParams(

LayoutParams.MATCH_PARENT, (MATCH_PARENT / 4) * 3));


The goal would be to make the component height be 3/4's of the width.


This doesn't have to be done from outside. i.e. it could be done from inside the view, but I'm unsure what I need to override inside my view implementation to set the height. Set it too early and i may not yet have my width, set it to late and I may just end up on a resize race loop :)


So, my question is, what is the proper way to make a View dynamically sized at runtime in at least one of the dimensions?


- Brill Pappin





--
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


--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com

--
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