繁体   English   中英

自定义视图的Android布局

[英]Android layouts for custom views

我正在尝试创建一个窗口,该窗口的顶部是自定义视图,底部是三个按钮。 我希望该视图占用创建按钮后剩下的不动产。 我的布局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>

然后,使用代码将我的自定义视图添加到窗口中

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

在onCreate方法中。

但是,自定义视图占据了整个屏幕! 如果我删除将自定义视图添加到框架的代码,则按钮栏将出现在窗口顶部(正如我期望的那样)。

我尝试覆盖onMeasure方法,添加

     super.onMeasure(widthMeasureSpec, heightMeasureSpec);

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

但这没有任何区别。

我已经尝试过布局宽度和高度的各种组合,这似乎并没有什么区别。

非常感谢所有帮助...

将FrameLayout android:layout_height更改为wrap_content。

还要添加到FrameLayout和Buttons的线性布局中android:layout_weight =“ 1.0”。

我希望这有帮助。

暂无
暂无

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

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