tags:

views:

41

answers:

3

Hi All,

I am developing a small app for Android. I am creating my own view.

I am planning to design the layout to be:

<LinearLayout>
   <MyView>
   <Button><Button>
</LinearLayout>

But when I run my program, my custom view will cover the whole screen. Is there anyway to avoid this?

I have already tried to add android:layout_width, layout_height to be wrap_content, fill_partent, but no luck.

+1  A: 

I usually put a FrameLayout where I want my custom view, then is code I create my view and add it to the FrameLayout.

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#0BF">

    <FrameLayout
        android:id="@+id/GLFrame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <FrameLayout
        android:id="@+id/ActivityFrame"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent" />

</FrameLayout>

And the access that from code like this.

FrameLayout glFrame = (FrameLayout)findViewById(R.id.GLFrame);
glFrame.addView(new NativeBackgroundSurface(this));
CaseyB
Thank you. I will give it a try
F.Z
F.Z
A: 

Assuming you want your layout to take up everything except for the space taken up by the buttons, you want to layout the buttons and then make your view align its parents top and layout above the button:

 <RelativeLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
>

<Button android:layout_width= whatever 
android:layout_height= whatever
    android:id="@+id/button"
    android:layout_alignParentBottom="true" />

<MyView android:layout_width="fill_parent" 
android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_above ="@id/button"/>

</RelativeLayout>
jqpubliq
Thank you! I will try that.
F.Z
A: 

In addition to the ways already suggested, here's another way. If you know what the size of this view will be, or you can programmatically determine its size, you can override the onMeasure method which tells the OS how big the view is. For example if you're writing a game or something that uses a Canvas then you can use the getHeight() and getWidth() methods, or you can get the size directly from whatever parameters you used when you created your canvas. onMeasure is easy to use:

@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
    setMeasuredDimension(width, height);
}

Refer to the documentation if you need a bit more info about the parameters. (If it doesn't quite make sense, don't worry, it didn't to me either :P But you can use this without actually needing to worry about widthMeasureSpec and heightMeasureSpec as long as you can calculate the size the view should be correctly.)

Steve H