0

我正在开发一个包含多个视图的自定义 ViewGroup。在构造函数中,我创建了许多 FrameLayout,每个都包含两个 ImageView。每个 Framelayout 和子视图都是使用以下代码创建的。

FrameLayout frame = new FrameLayout(context);

ImageView digitView = new ImageView(context);
digitView.setScaleType(ScaleType.CENTER_CROP);
frame.addView(digitView,
        new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));

ImageView bezelView = new ImageView(context);
bezelView.setScaleType(ScaleType.CENTER_CROP);
bezelView.setImageResource(R.drawable.bezel);
frame.addView(bezelView);


this.addView(frame, params); 

在 onMeasure 回调中,我设置了自定义 ViewGroup 的尺寸

        this.setMeasuredDimension(254, 43);

在 onLayout 回调中,我计算每个 FrameLayouts 的尺寸并为每个 FrameLayouts 调用布局方法

@Override
protected void onLayout (boolean changed, int left, int top, int right, int bottom)
{
    if (changed)
    {
        float width = (float)(right - left) / (float)this.digits;

        if ((int)(width/ASPECT_RATIO) > bottom- top)
            width = (float)(bottom - top) * ASPECT_RATIO;
        this.digitWidth = (int)width;
        this.digitHeight = (int)(width/ASPECT_RATIO);

        this.xOffset = ((right - left) - (this.digits * this.digitWidth))/2;
        this.yOffset = ((bottom - top) - this.digitHeight)/2;

        for (int i = this.digits-1; i >=0; i--)
        {
            int x = xOffset + i * this.digitWidth;
            this.placeFrames.get(i).layout(x, this.yOffset, x + this.digitWidth, yOffset + this.digitHeight); 

        }
    }
}

我遇到的问题是 FrameLayouts 在自定义 GroupView 中的大小和位置正确,但 ImageViews 没有被 FrameLayouts 调整大小 - 它们的宽度和高度为零。我使用 HierarchyViewer 验证了这一点。

我想我错过了一些东西 - 我假设 FrameLayout 上的布局方法会根据在将子 ImageView 作为子项添加到 FrameLayout 时传入的布局参数来调整子 ImageViews 的大小,但似乎不是。

非常感谢指向正确方向的指针。

谢谢

安德鲁

4

1 回答 1

0

我假设 FrameLayout 上的布局方法会根据作为子项添加到 FrameLayout 时传入的布局参数来调整子 ImageViews 的大小,但似乎不是。

你自己不是layout()这些 ImageView 对象,也不是调用super.onLayout()..

于 2012-11-14T20:56:23.253 回答