android – 扩展FrameLayout,子视图将不再显示

android – 扩展FrameLayout,子视图将不再显示,第1张

概述这是我的FrameLayout代码: <FrameLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/frameView" android:layout_width 这是我的FrameLayout代码: @H_403_2@<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中,我希望布局的高度始终是宽度的一半,所以我的代码是:

@H_403_2@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中使用它:

@H_403_2@<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.我想我也需要调整孩子们的观点.但我现在不知道该怎么做.

UPDATE

根据TheDimasig的评论,我刚检查了我的代码并更新了我的问题.谢谢

解决方法 最简单的方法是更改​​onMeasure中的measurespec,但仍然调用super: @H_403_2@@OverrIDe protected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) { int wIDth = MeasureSpec.getSize(wIDthMeasureSpec); int height = (int) (wIDth * 0.5); heightmeasureSpec = MeasureSpec.makeMeasureSpec(height,MeasureSpec.EXACTLY); super.onMeasure(wIDthMeasureSpec,heightmeasureSpec);}

这样您就可以获得所需的尺寸,但不必手动测量子尺寸.

总结

以上是内存溢出为你收集整理的android – 扩展FrameLayout,子视图将不再显示全部内容,希望文章能够帮你解决android – 扩展FrameLayout,子视图将不再显示所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1138616.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存