1
votes

I have a custom view that I'm trying to use in a vertical orientation with a textview/button combo (stuck in a horizontal LinearLayout) positioned below it on the screen. Currently my layout looks like this:

    <com.company.widgets.MyCustomView
        android:id="@+id/custom"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_alignParentTop="true"
        android:layout_weight="0.0" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|center_vertical"
        android:layout_marginTop="10dp"
        android:layout_alignParentBottom="true"
        android:layout_weight="1.0"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|left"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="5dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#000000"
            android:text="foobar" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|right"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="15dp"
            android:text="foobar" />
    </LinearLayout>

In my custom view I've overridden onMeasure() and onSizeChanged(). I take pains to ensure that my view's size doesn't exceed the measureSpec (for onMeasure) or the width/height values (for onSizeChanged) that it is passed. However, when my activity is laid out, the values passed to these two methods represent "the whole vertical height of the screen".

Consequently my custom view ends up making itself as tall as it wants to be and the button/textview make themselves "as tall as they need to be" (wrap_content). When these two heights exceed the height of the screen, the bottom portion of the custom view is drawn behind the button/textview.

My question: how should I modify the layout so that my custom view's onMeasure() and/or onSizeChanged() methods are passed a shorter height value? I'd like them to receive a value representing "screen height - height of the textview/button".

Note: if I change the layout so that the textview/button are positioned above the custom view, the height value I'm passed is correct (i.e. smaller than screen height).

1
I don't understand what your trying to accomplish, maybe include a screenshot. Also, you should consider changing the objects that make up your view instead of overriding the onMeasure and onSizeChanged. It sounds like you should look into a RelativeLayout instead of using LinearLayout. - Chris Feist
My custom view is actually a calendar widget that allows the user to tap on days. Since it's a custom view and I do the measuring & painting myself I really do need to override onMeasure() and onSizeChanged(). What I'm hoping to accomplish is for the textview/button combo to take up its desired height (wrap_content) and for the custom view to size itself to consume all the screen height not used by the textview/button combo. - jph

1 Answers

0
votes

Solved it. Make the outer element be a LinearLayout instead of Relative and reverse the weights. The custom view should have weight "1" and the LinearLayout that contains the TextView and Button should have weight zero. This causes the height value passed to my custom view's onMeasure() to be only the portion of the screen not taken up by the textview/button.