gpt4 book ai didi

android获取 Activity 中所有窗口的 View 层次结构

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:38:44 31 4
gpt4 key购买 nike

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

需求:获取当前 Activity 的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;
}

最佳答案

除了一些其他的反射魔法之外,我已经设法使用您的部分代码获得打开的微调器弹出 View 层次结构,这是完整的代码,请记住反射会影响您的 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;
}

关于android获取 Activity 中所有窗口的 View 层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22219089/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com