使用选项卡布局的问题 [英] problem with using tab layout

查看:89
本文介绍了使用选项卡布局的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我刚刚开始使用eclipse在android上进行编程.

我已经使用标签布局控件来设计表单.

现在我有3个标签和3个不同的活动.

我已经为此设计了布局XML.哪个显示一切都很好.

我的Java代码如下.

HI all,

I have just started programming on android using eclipse.

I have used tab layout control to design my form.

Now i have 3 tabs and 3 different activity.

I have already design my layout XML for this. Which displays everything pretty good.

My java code is as below.

TabHost th = (TabHost)findViewById(R.id.tabhost);
        th.setup();
        
        TabSpec specs = th.newTabSpec("Enquire");
        specs.setContent(R.id.tbenquire);
        specs.setIndicator("Enquire");
        
        th.addTab(specs);



我有3个这样的选项卡,但是问题是如何为他们分配自己的活动类别?

我在Google上发现使用了这样的内容




I have 3 different tabs like this, but the problem is how can i assign them their own activity class?

I found on google to use something like this


Intent intent = new Intent(this,Enquire.class);
th.setContent(intent);



但是那我怎么用"R.id.tbenquire"呢?

在此先感谢..



but then how can I use "R.id.tbenquire"?

Thanks in advance..

推荐答案

如果需要,您可以使用setContentView()在活动onCreate()中设置布局,就像处理其他任何布局一样活动.

尽管有两个原因,但这似乎不是推荐的使用选项卡视图的方法.
1.创建多个活动的开销.
2.如果选项卡之间具有依赖性,则在选项卡之间传递数据时会出现问题.
推荐的方法是将单个活动与多个视图一起使用.请注意,您仍然可以将代码设计为每个视图都有一个类(如果需要),以分隔行为.

如果以上两点对您来说都不成问题,那么如果您觉得更简单,请继续进行多项活动.我过去曾这样做,但意识到了传递状态的局限性.现在,我总是使用多个视图和一个活动.
If you want this, you can set the layout in your activitys onCreate() using setContentView(), just as you would with any other activity.

This does not seem to be the recommended way of using a tab view though, for two reasons.
1. The overhead of creating multiple activities.
2. Problems when passing data between the tabs if they have dependencies.
The recommended approach is to use a single activity with multiple views. Note, you can still design your code to have one class for each view, if that''s what you want, to separate behavior.

If the two points above is not presenting a problem for you, then go ahead with multiple activities if you feel that is easier. I did that in the past, but realized the limitations of passing state around. Now I alway use multiple views and a single activity.


这篇关于使用选项卡布局的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆