扩展的FrameLayout,子视图将不会显示再 [英] Extending FrameLayout, child views won't show anymore

查看:538
本文介绍了扩展的FrameLayout,子视图将不会显示再的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code与的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的表演很好。

The ImageView show well.

现在我有一个自定义布局从的FrameLayout,例如延长MyFrameLayout。

Now I have a custom layout extends from FrameLayout, e.g. MyFrameLayout.

在MyFrameLayout,我想布局的高度,始终是宽度的一半,所以我的code是:

In MyFrameLayout, I want the height of the layout is always half of the width, so my code is:

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

Then use it in 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消失了。

But now the inner ImageView disappeared.

我觉得我没有正确实施 onMeasure onLayout 。我想我需要调整孩子们的意见为好。但我不知道现在该做什么。

I think I don't implement onMeasure or onLayout correctly. I guess I need to resize the children views as well. But I don't know what to do now.

更新

TheDimasig 的评论,我只是检查我的code和更新我的问题。谢谢

Per TheDimasig's comment, I just checked my code and update my question. Thanks

推荐答案

最简单的方法是改变onMeasure的measurespec,但还是叫超:

The simplest way is to change the measurespec in onMeasure, but still call super:

 @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);
}

这样,你得到你想要的尺寸,但不必手动测量孩子。

This way you get the dimensions you want but don't have to manually measure the children.

这篇关于扩展的FrameLayout,子视图将不会显示再的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆