开发者

Android layouts for custom views

I'm trying to create a window with a custom view at the top, and three buttons at the bottom. I would like the view to take up the real estate which is left over after the buttons have been created. My layout XML is as follows

<LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff" >
  <FrameLayout
    android:id="@+id/CustomFrame"
    android:layout_width="fill_parent"
    android:layout_height开发者_C百科="fill_parent" />
  <LinearLayout
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:background="#ffffff" >
    <Button
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ok"
        android:layout_marginBottom="10dip" 
        android:layout_marginTop="10dip"  />
    <Button
        android:id="@+id/Button02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cancel"
        android:layout_marginBottom="10dip" 
        android:layout_marginTop="10dip"  />
    <Button
        android:id="@+id/Button03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/clear"
        android:layout_marginBottom="10dip" 
        android:layout_marginTop="10dip"  />
  </LinearLayout>
</LinearLayout>

I then add my custom view to the window with the code

  setContentView(R.layout.drawitwindow);
  FrameLayout layout = (FrameLayout)findViewById(R.id.CustomFrame);
  mView = new MyCustomView(this);
  layout.addView(mView);

in the onCreate method.

However, the custom view takes up the whole screen! If I remove the code which adds the custom view to the frame, the button bar appears at the top of the window (as I would expect).

I've tried overriding the onMeasure method, adding

     super.onMeasure(widthMeasureSpec, heightMeasureSpec);

     int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
     int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
     this.setMeasuredDimension(parentWidth,parentHeight);

but this doesn't make any difference.

I've tried various combinations of layout width and height, and again that doesn't seem to make any difference.

All help will be very gratefully received...


Change the FrameLayout android:layout_height to wrap_content.

Also add to the FrameLayout and to the Buttons' Linear Layout android:layout_weight="1.0".

I hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜