0

我想遍历所有视图的线性布局heiarchiy,但我在最后一个元素、框架布局及其正确的类中遇到异常。

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

0 回答 0