Understanding Activity setContentView

I need to create all the UI programmatically "on demand", which means that I can not use any XML. This is the pseudo code of what I am doing:

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); 

therefore, when a TabContentFactory is called, I return a view that is a view of the contents of the current activity. Basically what I am doing is the current view and porting it to tabhost. It works halfway, when I do this, I can see the tab, but only the black view below it, if I click on another tab and then click back, then I see the view, everything works as intended.
Now, why do I think this is due to setContentView , 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 works fine. Any help appreciated, thanks!

+4
source share
1 answer

Answering my own question, I found that it is not so when setContentView is setContentView , then the view is attached to the parent object and why it does not appear in tabhost. To remove the view from the parent, this code can be used:

 ViewGroup vg = (ViewGroup)(myView.getParent()); vg.removeView(myView); 
+4
source

Source: https://habr.com/ru/post/1314925/


All Articles