为什么我的TabHost的的FrameLayout唯一的孩子装能见度= View.GONE?我的、能见度、孩子、TabHost

2023-09-07 16:47:54 作者:凉城故人

我使用的是TabHost呈现一些标签。在XML定义所有设置了〜通常,一个RelativeLayout的是的FrameLayout的TabHost的唯一的孩子。

I'm using a TabHost to render some tabs. In the XML definition everything is set up ~normally, a RelativeLayout is the only child of the FrameLayout for the TabHost.

古怪,那相对布局总是加载了如View.GONE。设置RelativeLayout的的visibiltiy在XML文件中不改变这一点。的onCreate中调用.setVisibility手动进行了RelativeLayout的可见的作品,但感觉不对。标签之间的切换也使得RelativeLayout的再次可见。

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.

我缺少的东西根本?这是正常手工指定标签的的FrameLayout的XML孩子?

Am I missing something fundamental? Is it normal to hand-specify tabs as XML children of the FrameLayout?

推荐答案

我最终花一些时间阅读TabHost.java,发现发生了什么事。

I ended up spending some time reading TabHost.java and discovered what was happening.

TabHost.java(天真IMO))假设不同的选项卡连接到不同内容的看法

TabHost.java (naively IMO ;) ) assumes that different tabs are attached to different content views.

每一个内容视图添加到则tabspec视图的可见性设置为GONE时间。

Every time a content view is added to a TabSpec the visibility of the view is set to GONE.

每一个则tabspec加入到TabHost时,TabHost切换到标签0

Every time a TabSpec is added to the TabHost, the TabHost switches to tab 0.

每次setCurrentTab被称为该选项卡的视图的可见性设置为可见当且仅当在新选定的选项卡是不是当前选项卡。

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:

第一的观点,并将其可见性设置为走了, 那么TabHost切换到标签0和设置视图的可见为可见, 则下一个选项卡,并将其意见(同样的观点!)都有其可见设置为走了, 那么TabHost切换到标签0,这是一个〜空操作,因为标签0是previously选择这样的观点是不转回可见。

因此​​,这是通过使用相同的视图为多个选项卡的内容引起的。

So this was caused by using the same view for the content of multiple tabs.

所以,要解决这一呼吁: tabHost.getCurrentView()setVisibility(View.VISIBLE)。 ......将所有的选项卡后,

So, to fix this call: tabHost.getCurrentView().setVisibility(View.VISIBLE); ... after adding all the tabs