【发布时间】:2012-09-11 08:06:16
【问题描述】:
这是我的代码FrameLayout:
<FrameLayout
android:layout_width="fill_parent" android:layout_height="wrap_content">
<ImageView
android:id="@+id/frameView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/image1"
/>
</FrameLayout>
ImageView 显示良好。
现在我有一个从 FrameLayout 扩展的自定义布局,例如我的框架布局。
在MyFrameLayout中,我希望布局的高度总是宽度的一半,所以我的代码是:
public class MyFrameLayout extends FrameLayout {
// 3 constructors just call super
@Override
protected void onMeasure(int widthMeasureSpec,
int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = (int) (width * 0.5);
setMeasuredDimension(width, height);
}
}
然后在xml中使用:
<com.test.MyFrameLayout
android:layout_width="fill_parent" android:layout_height="wrap_content">
<ImageView
android:id="@+id/frameView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/image1"
/>
</com.test.MyFrameLayout>
但是现在内部的 ImageView 消失了。
我认为我没有正确实现onMeasure 或onLayout。我想我也需要调整子视图的大小。但我现在不知道该怎么办。
更新
根据 TheDimasig 的评论,我刚刚检查了我的代码并更新了我的问题。谢谢
【问题讨论】:
-
两个建议:1. 检查
MyFrameLayout类中的onMeasure和onLayout方法。 2.尝试使用hierarhyviewer工具进行调试。另外,是否可以显示自定义布局的所有代码 -
@TheDimasig,我刚刚更新了我的问题,请查看:)