开发者

Android TabHost Problem

I use android tabs in my application. I have a main class which i import my Tabs over there. There are 4 tabs so i have 1 java class for each. But in each tab i also go to other intents. When i move to other intent from one tab, the tab view disappears. I want my tabs to stay in all classes and in all views. What should i do for that. So th开发者_开发百科is is my code for main tab class.

Intent intent1; Intent intent2; Intent intent3; Intent 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);


hope this code may help you....

xml file :

<?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>

activity:

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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜