在设备上打印视图层次结构 [英] print a view hierarchy on a device

查看:62
本文介绍了在设备上打印视图层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我没有物理访问权限的三星手机上调试我的应用程序时,结果很奇怪.我想请用户运行一个有工具的应用程序来帮助调试.我的应用程序得到一个view,其中具有一个未知的层次结构(对我而言;-)(ViewGroups等).

Debugging my app with strange results on Samsung phones, which I don't have physical access to. I'd like to ask a user to run an instrumented App to help debug. My App gets a view that has an unknown (to me ;-) hierarchy in it (ViewGroups etc). Is there a way to "walk" a View and print out to a string / ddms all the components in the View (etc ViewGroups in it etc)?

这类似于HierarchyViewer工具-如果我可以adb级访问设备.

This would be akin to HierarchyViewer tool - if I had adb level access to the device.

更新:我想我可以使用方法

Update: I guess I could use the method

void dumpViewHierarchyWithProperties(Context context, ViewGroup group, BufferedWriter out, int level)

来自ViewDebug.java Android OS来源...

from the ViewDebug.java Android OS sources ...

有人有更好的主意吗?

谢谢!

推荐答案

以下是我为此目的创建的实用程序功能:

Here's the utility function I just made for this purpose:

public static void printViewHierarchy(ViewGroup vg, String prefix) {
    for (int i = 0; i < vg.getChildCount(); i++) {
        View v = vg.getChildAt(i);
        String desc = prefix + " | " + "[" + i + "/" + (vg.getChildCount()-1) + "] "+ v.getClass().getSimpleName() + " " + v.getId();
        Log.v("x", desc);

        if (v instanceof ViewGroup) {
            printViewHierarchy((ViewGroup)v, desc);
        }
    }
}

这篇关于在设备上打印视图层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆