如何在android中添加tabHost in Subactivity?

时间:2013-01-31 08:58:19

标签: android

我想在SubActivity中添加TabHost。

E.g。我有一个名为MainActivity的Activity,它代表一个TabHost。      我的tabHost中有五个标签。在每个tabHost上我打开活动A,B,C,D,E。

 Now when I want move from Activity A to some different activity e.g Z.
 then I am not able to add TabHost into Z. It's totally disappeared from Activity Z.

 So Is there any solution for this issue ? Please help me.

这是MainActivity Source:

public class MainActivity extends TabActivity {

    public static final String TAG_1 = "tab1";
    public static final String TAG_2 = "tab2";
    public static final String TAG_3 = "tab3";
    public static final String TAG_4 = "tab4";
    public static final String TAG_5 = "tab5";

    public TabHost mTabHost;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mTabHost = getTabHost();
        setTabs();
    }

    public void setTabs() {
        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup();

        addTab("Home", TAG_1, createTabDrawable(R.drawable.home),  FeedBack.class);
        addTab("Near Me", TAG_2, createTabDrawable(R.drawable.search), NearMe.class);
        addTab("Share", TAG_3, createTabDrawable(R.drawable.star),Home.class);
        addTab("FeedBack", TAG_4, createTabDrawable(R.drawable.settings),FeedBack.class);
        addTab("Options", TAG_5, createTabDrawable(R.drawable.settings),NearMe.class);


    }

    public Drawable createTabDrawable(int resId) {
        Resources res = getResources();
        StateListDrawable states = new StateListDrawable();

        final Options options = new Options();
        options.inPreferredConfig = Config.ARGB_8888;

        Bitmap icon = BitmapFactory.decodeResource(res, resId, options);

        Bitmap unselected = TabBitmap.createUnselectedBitmap(res, icon);
        Bitmap selected = TabBitmap.createSelectedBitmap(res, icon);

        icon.recycle();

        states.addState(new int[] { android.R.attr.state_selected }, new BitmapDrawable(res, selected));
        states.addState(new int[] { android.R.attr.state_enabled }, new BitmapDrawable(res, unselected));

        return states;
    }

    public View createTabIndicator(String label, Drawable drawable) {
        View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, mTabHost.getTabWidget(), false);

        TextView txtTitle = (TextView) tabIndicator.findViewById(R.id.text_view_tab_title);
        txtTitle.setText(label);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) txtTitle.getLayoutParams();
        txtTitle.setLayoutParams(params);

        ImageView imgIcon = (ImageView) tabIndicator.findViewById(R.id.image_view_tab_icon);
        imgIcon.setImageDrawable(drawable);

        return tabIndicator;
    }

    public void addTab(String label, String tag, Drawable drawable, Class<?> c) {
        Intent intent = new Intent(this, c);
        TabHost.TabSpec spec = mTabHost.newTabSpec(tag);
        spec.setIndicator(createTabIndicator(label, drawable));
        spec.setContent(intent);

        mTabHost.addTab(spec);
    }
}

2 个答案:

答案 0 :(得分:1)

Pravin,对于您正在寻找的任务,可以使用ActivityGroup

查看此链接,希望您能找到答案。

http://www.gamma-point.com/content/android-how-have-multiple-activities-under-single-tab-tabactivity

答案 1 :(得分:0)

试试这个,它对我有用.. 在你的主要活动的onCreate()

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

    etSearch = (EditText)findViewById(R.id.etSearch);

    TabHost tabhost = getTabHost();

    TabSpec first = tabhost.newTabSpec("All Submissions");        
    first.setIndicator("All Submissions"     ,getResources().getDrawable(R.drawable.submission));
    Intent intent1 = new Intent(this, AllSubmissionActivity.class);
    first.setContent(intent1);
    //tabhost.addTab(first);

    TabSpec second = tabhost.newTabSpec("All Districts");        
    second.setIndicator("All Districts"  ,getResources().getDrawable(R.drawable.district));
    Intent intent2 = new Intent(this, AllDistrictActivity.class);
    second.setContent(intent2);
    //tabhost.addTab(second);

    TabSpec third = tabhost.newTabSpec("All Schools");
    third.setIndicator("All Schools" ,getResources().getDrawable(R.drawable.school));
    Intent intent3 = new Intent(this, AllSchoolActivity.class);
    third.setContent(intent3);
    //tabhost.addTab(third);  

    if(DataContainer.UserCredentials.uname.equals("sadmin"))
    {
        tabhost.addTab(first);
        tabhost.addTab(second);
        tabhost.addTab(third); 
    }
    else if(DataContainer.UserCredentials.uname.equals("b"))
    {
        tabhost.addTab(second);
        tabhost.addTab(third); 
    }
    else
    {
        tabhost.addTab(third); 
    }


    tabhost.setCurrentTab(0); 

}

您必须按照相同的步骤在子活动中添加标签,如在主...

在子活动中

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.allsubmission);

    initializeTabHost();

}

这是initializeTabHoast()方法..

public void initializeTabHost(){



    tabhost = getTabHost();
    TabSpec first = tabhost.newTabSpec("Details"); 

    first.setIndicator("Details" ,null);
    Intent intent1 = new Intent(this, DetailsCrimeOnLink.class);

    intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);


    first.setContent(intent1);
    tabhost.addTab(first);

    TabSpec second = tabhost.newTabSpec("Maps");        
    second.setIndicator("Maps" ,null);
    Intent intent2 = new Intent(this, MapsCrimeOnLink.class);
    second.setContent(intent2);
    tabhost.addTab(second);

    TabSpec third = tabhost.newTabSpec("Notes");
    third.setIndicator("Notes" ,null);
    Intent intent3 = new Intent(this, NotesCrimeOnLink.class);
    third.setContent(intent3);
    tabhost.addTab(third); 

    TabSpec fourth = tabhost.newTabSpec("History");
    fourth.setIndicator("History" ,null);
    Intent intent4 = new Intent(this, HistoryCrimOnLink.class);
    fourth.setContent(intent4);
    tabhost.addTab(fourth);  

    tabhost.setCurrentTab(0);

            }