卡口与1活性不同的看法 [英] tabs with 1 activity different views

查看:241
本文介绍了卡口与1活性不同的看法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有上显示2个选项卡1活动。

I want to have 1 activity that is displayed on 2 tabs.

在我的主要我有以下几点:

in my main I have the following:

    TabHost tabHost = getTabHost();  // The activity TabHost

    tabHost.addTab(tabHost.newTabSpec("tab0").setIndicator(tabNames[0]).setContent(R.id.tab0));
    tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator(tabNames[1]).setContent(R.id.tab1));

    Intent intent = new Intent().setClass(this, DMXControllerActivity.class);
    startActivity(intent);

在我main.xml中我有这个里面的FrameLayout:

in my main.xml I have this inside the FrameLayout:

   <LinearLayout
       android:id="@+id/tab0"
       android:orientation="vertical"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:padding="5dp">
    </LinearLayout>

   <LinearLayout
       android:id="@+id/tab1"
       android:orientation="vertical"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:padding="5dp">
    </LinearLayout>

在我的活动我有这样的:

In my activity I have this:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    layoutTab0();
    layoutTab1();

}

私人无效layoutTab0(){

private void layoutTab0() {

   LinearLayout ll_main = new LinearLayout(this);
   ll_main.setOrientation(LinearLayout.VERTICAL);

    LinearLayout myll = (LinearLayout) findViewById(R.id.tab0);

    myll.addView(ll_main);

    LinearLayout ll_sliders = new LinearLayout(this);
    ll_sliders.setOrientation(LinearLayout.VERTICAL);
    ll_main.addView(ll_sliders);

......
.....
.....

...... ..... .....

}

当我启动应用程序,我看到彼此顶部我TAB0和TAB1的内容。当我打在模拟器中返回键然后我看到2标签如预期,但它们是空的。

when I start the app I see the contents of my tab0 and tab1 on top of each other. When I hit the back key in the emulator I then see the 2 tabs as expected but they are empty.

我如何得到我的标签内容,在标签中显示?

How do I get my tabs contents to show up in on the tabs?

推荐答案

您已经设置内容查看后才能执行此code:

You have to execute this code after setting the ContentView:

TabHost tabHost = getTabHost();  // The activity TabHost
tabHost.addTab(tabHost.newTabSpec("tab0").setIndicator(tabNames[0]).setContent(R.id.tab0));
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator(tabNames[1]).setContent(R.id.tab1));

更改您的onCreate()为:

Change your onCreate() to:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    TabHost tabHost = getTabHost();  // The activity TabHost
    tabHost.addTab(tabHost.newTabSpec("tab0").setIndicator(tabNames[0]).setContent(R.id.tab0));
    tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator(tabNames[1]).setContent(R.id.tab1));

    layoutTab0();
    layoutTab1();
}

在您的code你创建的标签,然后打开一个新的活动。如果你不打开活动选项卡将是present但他们将是空的,因为你没有将内容添加到他们。你不必调用一个新的活动,只需将内容添加到tablayouts,它应该工作。

In your code you're creating the tabs and then opening a new activity. If you don't open the activity the tabs will be present but they will be empty because you didn't add the content to them. You don't have to call a new activity, just add the content to the tablayouts and it should work.

这篇关于卡口与1活性不同的看法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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