get view group, need to be improved.
public class HelloworldActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); FrameLayout decor = (FrameLayout)getWindow().getDecorView(); dumpViewGroup(decor); } LinkedList <View> gViewQueue = new LinkedList<View>(); int gLevel = 0; public void dumpViewGroup(ViewGroup vg) { final int count = vg.getChildCount(); //Log.d(TAG, "lyftest count="+count); for (int i = 0; i < count; i++) { final View child = vg.getChildAt(i); //if (child instanceof ViewGroup) showViewName(child,gLevel); gViewQueue.add(child); } gLevel++; showlevelmark(gLevel); while(!gViewQueue.isEmpty()) { View headview= gViewQueue.remove(); if(headview instanceof ViewGroup) { dumpViewGroup((ViewGroup)headview); } } } public void showlevelmark(int level) { } public void showViewName(View v, int level) { Log.d("TAG", " :"+level+":view="+v+"(parent:"+v.getParent()+")"); } }
result:
09-03 16:16:23.906: DEBUG/TAG(893): :0:view=android.widget.LinearLayout@40517d70(parent:com.android.internal.policy.impl.PhoneWindow$DecorView@40517530)
09-03 16:16:23.906: DEBUG/TAG(893): :1:view=android.widget.FrameLayout@40518770(parent:android.widget.LinearLayout@40517d70)
09-03 16:16:23.906: DEBUG/TAG(893): :1:view=android.widget.FrameLayout@40519af8(parent:android.widget.LinearLayout@40517d70)
09-03 16:16:23.906: DEBUG/TAG(893): :2:view=android.widget.TextView@40518d60(parent:android.widget.FrameLayout@40518770)
09-03 16:16:23.906: DEBUG/TAG(893): :3:view=android.widget.LinearLayout@40519fc8(parent:android.widget.FrameLayout@40519af8)
09-03 16:16:23.906: DEBUG/TAG(893): :4:view=android.widget.TextView@4051a230(parent:android.widget.LinearLayout@40519fc8)
package elfylin.helloworld; import java.util.Queue; import java.util.LinkedList; import android.util.Log; import android.view.View; import android.view.ViewGroup; public class DumpViewGroup { static class ViewElement { public ViewElement(View view, int l) { v = view; level = l; } View v; int level; } //static LinkedList <View> gViewQueue = new LinkedList<View>(); //static int gLevel = 0; static public void DumpViewGroup() { } static public void LevelDump(View vg, int level) { Queue viewQueue= new LinkedList<ViewElement>(); //ViewElement element = new ViewElement(vg, level); viewQueue.add(new ViewElement(vg,level)); while (!viewQueue.isEmpty()) { ViewElement head = (ViewElement) viewQueue.remove(); showViewName(head.v,head.level); if (head.v instanceof ViewGroup) { //level = level+1; for (int i = 0; i < ((ViewGroup)(head.v)).getChildCount(); i++) { final View child = ((ViewGroup)(head.v)).getChildAt(i); //if (child instanceof ViewGroup) //showViewName(child, level); viewQueue.add(new ViewElement(child,head.level+1)); } } } } static public void DepthDump(View vg, int level) { showViewName(vg, level); if (vg instanceof ViewGroup) { for (int i =0; i< ((ViewGroup)vg).getChildCount();i++) { View child = ((ViewGroup)vg).getChildAt(i); DepthDump(child,level+1); } } } public static void showlevelmark(int level) { } public static void showViewName(View v, int level) { Log.d("TAG", "lyftest :"+level+":"+(v.findFocus()!=null)+":"+(v.getVisibility())+":"+v+"( parent:"+v.getParent()+")"); } }