自定义视图的Android布局
问题描述:
我想创建一个窗口,顶部有一个自定义视图,底部有三个按钮。我希望视图能够占用按钮创建后留下的房地产。我的布局XML是如下自定义视图的Android布局
<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>
我再加入我的自定义视图的窗口,并在onCreate方法的代码
setContentView(R.layout.drawitwindow);
FrameLayout layout = (FrameLayout)findViewById(R.id.CustomFrame);
mView = new MyCustomView(this);
layout.addView(mView);
。
但是,自定义视图占据整个屏幕!如果我删除将自定义视图添加到框架的代码,则按钮栏会出现在窗口的顶部(如我所期望的那样)。
我试图重写onMeasure方法,加入
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
this.setMeasuredDimension(parentWidth,parentHeight);
但是这并没有什么差别。
我试过布局宽度和高度的各种组合,并且似乎没有任何区别。
所有的帮助将非常感激地接受......
答
更改的FrameLayout安卓layout_height为wrap_content。
还添加到FrameLayout和按钮的线性布局android:layout_weight =“1.0”。
我希望这会有所帮助。
芮,感谢您的提示。这样做会让事情变得更好,但并不完全解决问题 - 按钮现在出现,但它们具有不正确的(小)高度,因此文本不显示。 我浏览了几个附录,发现了一个可行的解决方案。不幸的是,由于我的空间不足,我无法发布新的XML!基本上,我在顶层使用了一个相对布局,正如问题“如何在android中的视图中建立自定义视图” 我不知道为什么我之前没有找到这个主题;在问我的问题之前,我花了几个小时寻找解决方案。 –
是的,相对布局更易于使用。如果您在每个布局上都使用了layout_weight,它就会起作用。 –
谢谢,瑞 - 我会回到线性布局并使用layout_weight来玩。这一切都有助于理解。 –