Android TabHost问题

时间:2011-07-13 07:55:34

标签: android android-tabhost

我在我的应用程序中使用android选项卡。我有一个主要类,我在那里导入我的标签。有4个标签,所以每个都有1个java类。但在每个标签中我也会转向其他意图。当我从一个选项卡移动到其他意图时,选项卡视图消失。我希望我的标签保留在所有类和所有视图中。我该怎么办呢。所以这是我的主要标签类的代码。

意图intent1;         意图intent2;         意图3;         意图intent4;

    intent1 = new Intent().setClass(this, x.class);  

    spec = tabHost
            .newTabSpec("x")
            .setIndicator("x",


    tabHost.addTab(spec);

    intent2 = new Intent().setClass(this, y.class);
    spec = tabHost
            .newTabSpec("y")
            .setIndicator("y",


    tabHost.addTab(spec);

    intent3 = new Intent().setClass(this,z.class);
    spec = tabHost
            .newTabSpec("z")
            .setIndicator("z",


    tabHost.addTab(spec);

    intent4 = new Intent().setClass(this, a.class);
    spec = tabHost
            .newTabSpec("a")
            .setIndicator("a",


    tabHost.addTab(spec);

1 个答案:

答案 0 :(得分:2)

希望此代码可以帮助您....

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:admobsdk="http://schemas.android.com/apk/res/com.admob.android.example"   
    android:orientation="vertical"      
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >



<LinearLayout 
    android:id="@+id/llTab"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:visibility="visible"
    android:layout_weight="2">

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:focusable="false"
        android:focusableInTouchMode="false">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"                 
            android:background="@android:color/darker_gray"             
            android:nextFocusUp="@android:id/tabcontent"
            android:layout_gravity="bottom"
            android:focusable="false"
            android:focusableInTouchMode="false"
            />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:paddingBottom="65dp"
            android:focusable="false"
            android:focusableInTouchMode="false"/>

    </TabHost>

</LinearLayout>

<强>活性

public class ActivityGroup extends TabActivity {
/** Called when the activity is first created. */

    private static final String TAG_TYERS = "Tab1";
    private static final String TAG_BABES = "Tab2";
    private static final String TAG_TEAMS = "Tab3";
    private static final String TAG_EVENTS = "Tab4";
    TabHost tabHost; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tabHost = getTabHost();

    tabHost.addTab(tabHost.newTabSpec("Tab1")
        .setIndicator(TAG_TYERS)
        .setContent(new Intent().setClass(this, Tab1.class)));

    tabHost.addTab(tabHost.newTabSpec("Tab2")
        .setIndicator(TAG_BABES)
        .setContent(new Intent().setClass(this, Tab2.class)));

    tabHost.addTab(tabHost.newTabSpec("Tab3")
        .setIndicator(TAG_EVENTS)
        .setContent(new Intent().setClass(this, Tab3.class)));

    tabHost.addTab(tabHost.newTabSpec("Tab4")
        .setIndicator(TAG_EVENTS)
        .setContent(new Intent().setClass(this, Tab4.class)));

    tabHost.setCurrentTab(0);
}