这是我的代码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。
在 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的评论,我刚刚检查了我的代码并更新了我的问题。谢谢