Android Tab 初始化问题
我在 Android 上的选项卡视图上遇到问题。
我使用 4 个 xml 布局文件(每个都是relativelayout)作为选项卡视图中选项卡的内容。
main.xml 包含: TabHost (@android:id/tabhost) 包含一个 LinearLayout,其中包含一个 TabWidget (@android:id/tabs) 和一个 FrameLayout (@android:id/tabcontent)
如果我将多个一个接一个地嵌入main.xml 中的一切工作正常...(除了我的 main.xml 无法维护,这是我想通过将文件拆分为一个简单的 main.xml 来解决的问题,该文件定义了选项卡和内容框架,然后推送对此的看法...)。
我必须膨胀并将 4 个relativelayout xml 文件插入到选项卡内容中的代码如下:
mTabHost = getTabHost();
View wv = null;
wv = LayoutInflater.from(this).inflate(R.layout.user_tab, mTabHost.getTabContentView(), true);
mTabHost.addTab(mTabHost.newTabSpec("User").setIndicator("User").setContent(wv.getId()));
wv = LayoutInflater.from(this).inflate(R.layout.track_tab, mTabHost.getTabContentView(), true);
mTabHost.addTab(mTabHost.newTabSpec("Track").setIndicator("Track").setContent(wv.getId()));
wv = LayoutInflater.from(this).inflate(R.layout.chart_tab, mTabHost.getTabContentView(), true);
mTabHost.addTab(mTabHost.newTabSpec("Chart").setIndicator("Chart").setContent(wv.getId()));
// 对于多个选项卡,依此类推。
当我运行此命令时,第一个选项卡(用户)为空,其余选项卡包含所有视图中的所有内容...因此 tab2 具有 tab1-4 中的内容,tab3 具有 tab1-4 中的内容,tab4 具有 tab4 中的内容从 tab1-4... 返回到 tab1,它现在拥有 tab1-4 中的所有内容。
代码工作得很好,视图中各个对象上的事件等都很好...只是它们在视图中都混乱在一起...
关于导致此问题的原因以及如何纠正它有什么想法吗?
提前致谢。
吉姆
I have a problem with a tab view on android.
I am using 4 xml layout files (each a RelativeLayout) as the content for tabs in a tab view.
The main.xml contains: TabHost (@android:id/tabhost) containing a linearLayout which contains a TabWidget (@android:id/tabs) and a FrameLayout (@android:id/tabcontent)
If I embedd the multiple one after another in the in the main.xml everything works fine... (except my main.xml is unmaintainable which is the problem i want to solve by splitting the files into a simple main.xml which defines the tab and the content frame and then push the views onto this...).
The code i have to inflate and insert the 4 RelativeLayout xml files into the tab content is as follows:
mTabHost = getTabHost();
View wv = null;
wv = LayoutInflater.from(this).inflate(R.layout.user_tab, mTabHost.getTabContentView(), true);
mTabHost.addTab(mTabHost.newTabSpec("User").setIndicator("User").setContent(wv.getId()));
wv = LayoutInflater.from(this).inflate(R.layout.track_tab, mTabHost.getTabContentView(), true);
mTabHost.addTab(mTabHost.newTabSpec("Track").setIndicator("Track").setContent(wv.getId()));
wv = LayoutInflater.from(this).inflate(R.layout.chart_tab, mTabHost.getTabContentView(), true);
mTabHost.addTab(mTabHost.newTabSpec("Chart").setIndicator("Chart").setContent(wv.getId()));
// And so on for the multiple tabs.
When i run this the first tab (User) is empty and the remaining tabs have all the content from all the views... So tab2 has the content from tab1-4, tab3 has the content from tab1-4 and tab4 has the content from tab1-4... Returning to tab1 it then has all the content from tab1-4 now.
The code works just fine, the events on the various objects in the views etc all are good... It is just that they are all muddled together in the view...
Any ideas on what is causing this and how to correct it?
Thanks in advance.
Jim
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,将:替换
为:
第二,将:替换
为:
关键是第三个参数中的
false
。First, replace:
with:
Second, replace:
with:
The key there being the
false
in third parameter.好吧...弄清楚为什么原来的方法不起作用...并修复了它...也是来自@CommonsWare的一个很好的解决方案(谢谢!)
原始代码的解决方案结果是这样的:
关键点似乎是需要将膨胀的视图添加到选项卡内容视图中,但是,传递给 setContent 的 id 必须是我刚刚膨胀的新选项卡视图。
因此,如果我用 true 调用 inflate,它会添加为子视图,但返回父视图(即 tabContent 框架)...如果这是我传递给 setContent 的 id,它会将所有对象放入该视图中在屏幕上...我原来的问题。
因此,但使用 false 调用会创建视图,但会跳过添加到父阶段,但重要的是,它会返回新视图。因此,如果我将此 tabContentView 添加到框架中:
然后在 setContent 中,将 id 设置为新视图的 id,c
Bingo:有效
感谢您的帮助!
Ok... Figured out why the original approach did not work... and fixed it... Also a great solution from @CommonsWare (Thanks!)
The solution for the original code turned out to be this:
The key point seems to be that the inflated view needs to be added to the tab content view, BUT, the id that is passed to setContent must be the new tab view I just inflated.
So, if I call inflate with true, it does the add as a sub view, but returns the parent view (i.e. the tabContent frame)... if this is the id i pass to setContent it puts all of the objects in that view on the screen... my original problem.
So, but calling with false creates the view but skips the add to parent stage, Critically however, it returns the NEW view. So, if i add this tabContentView to the frame with:
And then in the the setContent, set the id to the one for the new view, c
Bingo: works
Thanks for the help!