Why is my TabHost's FrameLayout's only child loaded with visibility = View.GONE?
I'm using a TabHost to render some tabs. In the XML definition everything is set up ~normally, a RelativeLayout is the only child o开发者_运维技巧f the FrameLayout for the TabHost.
Weirdly, that relative layout always loads up as View.GONE. Setting the RelativeLayout's visibiltiy in the XML file does not change this. Calling .setVisibility during onCreate to manually make that RelativeLayout visible works, but feels wrong. Switching between tabs also makes the RelativeLayout visible again.
Am I missing something fundamental? Is it normal to hand-specify tabs as XML children of the FrameLayout?
I ended up spending some time reading TabHost.java and discovered what was happening.
TabHost.java (naively IMO ;) ) assumes that different tabs are attached to different content views.
Every time a content view is added to a TabSpec the visibility of the view is set to GONE.
Every time a TabSpec is added to the TabHost, the TabHost switches to tab 0.
Every time setCurrentTab is called the visibility of the View of that tab is set to VISIBLE if and only if the newly selected tab is not the current tab.
When a bunch of tabs are added for the same view:
- first the view is added and its visibility is set to GONE,
- then the TabHost switches to tab 0 and sets the view's vis to VISIBLE,
- then the next tab is added and its view (the same view!) has its vis set to GONE,
- then the TabHost switches to tab 0 which is a ~noop because tab 0 was previously selected so the view isn't switched back to VISIBLE.
So this was caused by using the same view for the content of multiple tabs.
So, to fix this call: tabHost.getCurrentView().setVisibility(View.VISIBLE); ... after adding all the tabs
精彩评论