android:如何使用tabhost / tab小部件(通常是tabs)将不同的活动放在选项卡内容上

时间:2012-07-23 11:14:04

标签: android

首先! 我使用了标签导航类型使用了生成的Activity。我只是用它来Tab自动生成Activity控件。它在最新的 ADT 。我想:( ,, 现在我的问题是如何使用其他Fragments中的ActivityTab内容

其次, 有没有其他方法可以将另一个Activity放在Tab内容中,让我们说它的MainACtivity.class?....

计划包含3个Tab个3个Activity个,每个Activitytab个{{1}}

真的需要你的帮助,我已经用完了想法和来源:( :( ... 一种新的android开发所以要温柔。 :)

1 个答案:

答案 0 :(得分:1)

tab.xml:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
         >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:layout_marginTop="60dp"/>
       <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp"/>
    </RelativeLayout>

</TabHost>

在activity.java文件中添加以下代码:

将您的活动延长android.app.TabActivity而不是Activity

TabHost tabHost=getTabHost();
    TabHost.TabSpec spec;
    Intent intent;

    intent=new Intent().setClass(YourActivity.this, NewActivity.class);
    spec=tabHost.newTabSpec("tab1").setIndicator("imageId").setContent(intent);
    tabHost.addTab(spec);

    intent=new Intent().setClass(YourActivity.this, New1Activity.class);
    spec=tabHost.newTabSpec("tab2").setIndicator("imageId").setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);

以同样的方式,您可以在活动中添加任意数量的标签。