Jai*_*ken 6 android view-hierarchy
我正在为自动化制作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;
}
Run Code Online (Sandbox Code Playgroud)
除了一些其他反射魔法之外,我还设法使用部分代码获取打开的微调器弹出视图层次结构,这是完整的代码,请记住反射会影响 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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1886 次 |
| 最近记录: |