I need to create all the UI programmatically "on demand", this means I can't use any XML. 开发者_JAVA技巧This is the pseudo code of what I do:
View v = new MyView();
activity.setContentView(v);
tabHost = new TabHost();
....
tabHost.setup();
TabSpec tabSpec = _tabHost.newTabSpec(page);
tabSpec.setIndicator(title);
tabSpec.setContent((TabContentFactory) this);
activity.setContentView(tabHost);
so when TabContentFactory is called I return the view which is the content view of the current activity. Basically what I do is taking current view and wrapping it in the tabhost. It half works, when I do this I'm able to see the tabbar, but only black view below it, if I click on other tab and then click back then I can see the view, everything works as intended.
Now why I think it's related tosetContentView
, because when I do this:
View v = new MyView();
// activity.setContentView(v); // we don't use it as current content view
tabHost = new TabHost();
....
tabHost.setup();
TabSpec tabSpec = _tabHost.newTabSpec(page);
tabSpec.setIndicator(title);
tabSpec.setContent((TabContentFactory) this);
Then everything is working perfectly. Any help appreciated, thanks!
Answering my own question, I found what was wrong, when setContentView
is called then the view attaches to the parent and that's why it does not appear in the tabhost. To remove the view from it's parent this code can be used:
ViewGroup vg = (ViewGroup)(myView.getParent());
vg.removeView(myView);
精彩评论