6

我正在为自动化制作 SKD。所以我的需求与正常的应用程序开发略有不同。

要求:获取当前活动的 ViewHierarchy。 问题:当 Spinner 未打开时,我得到了正确的结果。当它打开时,我没有得到微调器的详细信息。

我使用以下代码来获取层次结构。问题是: Spinner 是否托管在不同的窗口中,这就是我没有得到它的原因?得到它的方法是什么?

    //This is how I start recursion to get view hierarchy
    View view = getWindow().getDecorView().getRootView();
    if (view instanceof ViewGroup) {
        ViewGroup group = (ViewGroup) view;
        dumpViewHierarchyWithProperties( group, 0);
    }

//Functions to get hierarchy
private  void dumpViewHierarchyWithProperties(ViewGroup group,int level) {
    if (!dumpViewWithProperties(group, level)) {
        return;
    }

    final int count = group.getChildCount();
    for (int i = 0; i < count; i++) {
        final View view = group.getChildAt(i);
        if (view instanceof ViewGroup) {
            dumpViewHierarchyWithProperties((ViewGroup) view, level + 1);
        } else {
            dumpViewWithProperties(view, level + 1);
        }
    }
}

private  boolean dumpViewWithProperties(View view,int level) {

    //Add to view Hierarchy.
    return true;
}
4

1 回答 1

1

除了一些其他反射魔法之外,我已经设法使用您的部分代码获得打开的微调器弹出视图层次结构,这是完整的代码,请记住反射会影响您的 SDK 和使用它的应用程序的性能。

//Function to get all available windows of the application using reflection
private void logRootViews() {
    try {
        Class wmgClass = Class.forName("android.view.WindowManagerGlobal");
        Object wmgInstnace = wmgClass.getMethod("getInstance").invoke(null, (Object[])null);

        Method getViewRootNames = wmgClass.getMethod("getViewRootNames");
        Method getRootView = wmgClass.getMethod("getRootView", String.class);
        String[] rootViewNames = (String[])getViewRootNames.invoke(wmgInstnace, (Object[])null);

        for(String viewName : rootViewNames) {
            View rootView = (View)getRootView.invoke(wmgInstnace, viewName);
            Log.i(TAG, "Found root view: " + viewName + ": " + rootView);
            getViewHierarchy(rootView);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

//Functions to get hierarchy
private void getViewHierarchy(View view) {
    //This is how I start recursion to get view hierarchy
    if (view instanceof ViewGroup) {
        ViewGroup group = (ViewGroup) view;
        dumpViewHierarchyWithProperties(group, 0);
    } else {
        dumpViewWithProperties(view, 0);
    }
}

private void dumpViewHierarchyWithProperties(ViewGroup group, int level) {
    if (!dumpViewWithProperties(group, level)) {
        return;
    }

    final int count = group.getChildCount();
    for (int i = 0; i < count; i++) {
        final View view = group.getChildAt(i);
        if (view instanceof ViewGroup) {
            dumpViewHierarchyWithProperties((ViewGroup) view, level + 1);
        } else {
            dumpViewWithProperties(view, level + 1);
        }
    }
}

private boolean dumpViewWithProperties(View view, int level) {
    //Add to view Hierarchy.
    if (view instanceof TextView) {
        Log.d(TAG, "TextView from hierarchy dumped: " + view.toString() + " with text: " + ((TextView) view).getText().toString() + " ,in Level: " + level);
    } else {
        Log.d(TAG, "View from hierarchy dumped: " + view.toString() + " ,in Level: " + level);
    }
    return true;
}
于 2017-05-23T15:19:05.017 回答