简体   繁体   English

自定义视图的Android布局

[英]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 我的布局XML如下

<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="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. 在onCreate方法中。

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 我尝试覆盖onMeasure方法,添加

     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. 将FrameLayout android:layout_height更改为wrap_content。

Also add to the FrameLayout and to the Buttons' Linear Layout android:layout_weight="1.0". 还要添加到FrameLayout和Buttons的线性布局中android:layout_weight =“ 1.0”。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM