我正在開發一個包含多個視圖的自定義ViewGroup。在構造函數中,我創建了多個FrameLayouts,每個FrameLayouts都包含兩個ImageView。每個Framelayout和子視圖都使用以下代碼創建。添加到FrameLayout的ImageViews具有零維
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的的尺寸,並要求每個
佈局方法@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沒有被F rameLayouts - 它們具有零寬度和高度。我使用HierarchyViewer驗證了這一點。
我想我已經錯過了一些東西 - 我認爲在FrameLayout上的佈局方法將負責根據傳入的佈局參數來調整子ViewView的大小,當它們被添加到FrameLayout作爲子節點時,但它似乎不是。
一個指針在正確的方向將非常感激。
感謝
安德魯
感謝的指針 - 您的評論使我在正確的區域看 - 我確實確實需要調用佈局在每個圖像的觀點。我也確定我不需要封閉的FrameLayout,因爲我可以將ImageView直接放置在自定義ViewGroup的範圍內。我想你的意思是我也應該調用'super.layout()',而不是'super.onLayout()'。謝謝 – drew