在LinearLayout视图中遍历所有子节点时获取ClassCastException?

时间:2012-12-14 11:30:23

标签: android

我希望遍历所有视图的线性布局,但我在最后一个元素,框架布局和正确的类中得到异常。

public void traverseRootsViewAndSetLayoutParam(View view,
        double heightRatio, double widthRatio) {
    view.setLayoutParams(new LinearLayout.LayoutParams(
            (int) (((double) widthRatio) * (view.getWidth())),
            (int) (((double) heightRatio) * (view.getHeight()))));
    if (view instanceof LinearLayout || view instanceof FrameLayout) {
        if (view instanceof LinearLayout) {
            System.out.println(">>> LinearLayout Class >>> "
                    + view.getClass());
            int childcount = ((LinearLayout) view).getChildCount();
            for (int i = 0; i < childcount; i++) {
                View childView = ((LinearLayout) view).getChildAt(i);
                traverseRootViewAndSetLayoutParam(childView, heightRatio,
                        widthRatio);
            }
        } else if (view instanceof FrameLayout) {
            System.out.println(">>> FrameLayout Class >>> "
                    + view.getClass());
            // getting error here , even the view is framelayout???
            int childcount = ((FrameLayout) view).getChildCount();
            for (int i = 0; i < childcount; i++) {
                View childView = ((FrameLayout) view).getChildAt(i);
                traverseRootViewAndSetLayoutParam(childView, heightRatio,
                        widthRatio);
            }
        }
    }
}

0 个答案:

没有答案