开发者

Tabhost in the action bar of Honeycomb app?

开发者 https://www.devze.com 2023-03-15 13:53 出处:网络
I have an app (for Honeycomb) with a main activity that shows a sort of dashboard, with three buttons and a title. When the user clicks a button they are taken to a screen where they can enter data an

I have an app (for Honeycomb) with a main activity that shows a sort of dashboard, with three buttons and a title. When the user clicks a button they are taken to a screen where they can enter data and do a calculation. I would like to have two appr开发者_如何学Coaches to the calculation in this second ('calculator') activity, and would like to implement this through having two tabs in the action bar (only when you are in this calculator activity).

I haven't used a tabhost widget or tabs ever before, so how do I go about having a tab widget in the action bar and changing the rest of the screen (everything but the action bar and system bar) when the other tab is selected?

If someone could point me towards some source code specifically for Honeycomb action bar tabs, that would be great.

Thanks for any help, and have a great day.


See Honycomb Gallery which makes use of action bar tabs.


Tabs in the action bar is a very neat feature. To make this question complete here on SO, I'll provide an example; This code goes in your Activity's onCreate

final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

// remove the activity title to make space for tabs
actionBar.setDisplayShowTitleEnabled(false);


// instantiate some fragments for the tabs
Fragment fragment1 = new Fragment1();
Fragment fragment2 = new Fragment2();

// add a new tab and set its title text and tab listener
actionBar.addTab(actionBar.newTab().setText(R.string.title_tab1)
                .setTabListener(new MyTabListener(fragment1)));

actionBar.addTab(actionBar.newTab().setText(R.string.title_tab2)
                .setTabListener(new MyTabListener(fragment2)));

You can put the MyTablListener as an inner class of your activity, It could look something like this;

class MyTabListener implements ActionBar.TabListener {
    private Fragment fragment;

    public MyTabListener(Fragment fragment) {
        this.fragment = fragment;
    }

    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        ft.replace(R.id.activity_new_formula_fragment_content, fragment, null);
    }

    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
        ft.remove(fragment);
    }

    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
    }
}
0

精彩评论

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