简体   繁体   中英

Android Add content to Custom Tab

I used this link to make my tabpages. And I want to make this . But I have no idea how to add content to the customized pages.

I've read a lot but found no solution. Thanks

NOTICE:

Below code works only in 2.3.3 or above. If you will try in lower API level,It will give you "default tabhost" like look,with gray line under each tab.


Try this:

public class CustomTabActivity extends TabActivity {

    private TabHost mTabHost;

    private void setupTabHost() {
        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup();       
    }

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

        setupTabHost();
        mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);

        setupTab(new TextView(this), "Tab 1");
        setupTab(new TextView(this), "Tab 2");
        setupTab(new TextView(this), "Tab 3");
        mTabHost.setCurrentTab(2);
    }

    private void setupTab(final View view, final String tag) {
        View tabview = createTabView(mTabHost.getContext(), tag);

        TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new Intent(this,Activity1.class));
        mTabHost.addTab(setContent);
    }

    private static View createTabView(final Context context, final String text) {
        View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
        TextView tv = (TextView) view.findViewById(R.id.tabsText);
        tv.setText(text);
        return view;
    }   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM