大佬教程收集整理的这篇文章主要介绍了android – 扩展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.我想我也需要调整孩子们的观点.但我现在不知道该怎么做.
@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,子视图将不再显示所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。