下面列出了怎么用android.support.v4.app.FragmentTabHost的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_connection, container, false);
Bundle bundle = new Bundle();
bundle.putString(ActivityConstants.CONNECTION_KEY, connection.handle());
// Initialise the tab-host
mTabHost = (FragmentTabHost) rootView.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);
// Add a tab to the tabHost
mTabHost.addTab(mTabHost.newTabSpec("Messages").setIndicator("Messages"), HistoryFragment.class, bundle);
mTabHost.addTab(mTabHost.newTabSpec("I/O").setIndicator("I/O"), PublishFragment.class, bundle);
//mTabHost.addTab(mTabHost.newTabSpec("Subscribe").setIndicator("Subscribe"), SubscriptionFragment.class, bundle);
mTabHost.addTab(mTabHost.newTabSpec("Tutorial").setIndicator("Tutorial"), TutorialFragment.class, bundle);
return rootView;
}
private void initTab() {
HomeFragment.setMainActivity(this);
KyTab tab_home = new KyTab(HomeFragment.class, "主页", R.drawable.navigationbar_selector_home);
KyTab tab_hot = new KyTab(HotFragment.class, "便民", R.drawable.navigationbar_selector_hot);
KyTab tab_category = new KyTab(CategrayFragment.class, "超市", R.drawable.navigationbar_selector_category);
KyTab tab_cart = new KyTab(CartFragment.class, "购物车", R.drawable.navigationbar_selector_cart);
KyTab tab_user = new KyTab(UserFragment.class, "个人", R.drawable.navigationbar_selector_user);
kyTabs.add(tab_home);
kyTabs.add(tab_hot);
kyTabs.add(tab_category);
kyTabs.add(tab_cart);
kyTabs.add(tab_user);
fragmentTabHost = (FragmentTabHost) findViewById(R.id.fragmentTabHost);
fragmentTabHost.setup(mContext, getSupportFragmentManager(), R.id.fragment);
for (KyTab tab : kyTabs) {
TabHost.TabSpec tabSpec =
fragmentTabHost.newTabSpec(tab.getTitle()).setIndicator(buildIndicator(tab));
fragmentTabHost.addTab(tabSpec, tab.getFragment(), null);
}
fragmentTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
fragmentTabHost.setCurrentTab(0);
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Fabric.with(this, new Crashlytics());
setContentView(R.layout.activity_recording);
tabHost = (FragmentTabHost)findViewById(R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
this.addTab(ReadingFragment.class, getString(R.string.title_text));
this.addTab(RecordGraphFragment.class, getString(R.string.realtime_graph));
SharedPreferences sharedPref =
getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
Integer tabIndex = sharedPref.getInt(getString(R.string.recording_tab_index), 0);
tabHost.setCurrentTab(tabIndex);
}
@Override
protected void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_login );
Backendless.initApp( this, Defaults.APP_ID, Defaults.API_KEY );
Bundle readBundle = new Bundle( );
readBundle.putString( Defaults.ROLE_TAG, Defaults.READ_ROLE );
Bundle writeBundle = new Bundle( );
writeBundle.putString( Defaults.ROLE_TAG, Defaults.READ_WRITE_ROLE );
mTabHost = (FragmentTabHost) findViewById( android.R.id.tabhost );
mTabHost.setup( this, getSupportFragmentManager(), R.id.tabFrameLayout );
mTabHost.addTab( mTabHost.newTabSpec( "read" ).setIndicator( "ReadRole" ), LoginFragment.class, readBundle );
mTabHost.addTab( mTabHost.newTabSpec( "write" ).setIndicator( "ReadWriteRole" ), LoginFragment.class, writeBundle );
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_tabs);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
FragmentStackSupport.CountingFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
LoaderCursorSupport.CursorLoaderListFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
LoaderCustomSupport.AppListFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),
LoaderThrottleSupport.ThrottledLoaderListFragment.class, null);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.fragment1);
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
FragmentStackSupport.CountingFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
LoaderCursorSupport.CursorLoaderListFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
LoaderCustomSupport.AppListFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),
LoaderThrottleSupport.ThrottledLoaderListFragment.class, null);
return mTabHost;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTabHost = new FragmentTabHost(this);
setContentView(mTabHost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.fragment1);
mTabHost.addTab(mTabHost.newTabSpec("menus").setIndicator("Menus"),
FragmentMenuFragmentSupport.class, null);
mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
LoaderCursorSupport.CursorLoaderListFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("stack").setIndicator("Stack"),
FragmentStackFragmentSupport.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tabs").setIndicator("Tabs"),
FragmentTabsFragmentSupport.class, null);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_display_novel_tab, container, false);
mTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost);
mTabHost.setup(getSherlockActivity(), getChildFragmentManager(), R.id.content);
mTabHost.addTab(mTabHost.newTabSpec(MAIN_SPEC).setIndicator(MAIN_SPEC),
DisplayLightNovelListFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec(TEASER_SPEC).setIndicator(TEASER_SPEC),
DisplayTeaserListFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec(ORIGINAL_SPEC).setIndicator(ORIGINAL_SPEC),
DisplayOriginalListFragment.class, null);
return view;
}
private void initTabHost() {
//实例化FragmentTabHost对象
FragmentTabHost fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
fragmentTabHost.setup(this,getSupportFragmentManager(),android.R.id.tabcontent);
//去掉分割线
fragmentTabHost.getTabWidget().setDividerDrawable(null);
for (int i = 0; i<mTableItemList.size(); i++) {
TabItem tabItem = mTableItemList.get(i);
//实例化一个TabSpec,设置tab的名称和视图
TabHost.TabSpec tabSpec = fragmentTabHost.newTabSpec(tabItem.getTitleString()).setIndicator(tabItem.getView());
fragmentTabHost.addTab(tabSpec,tabItem.getFragmentClass(),null);
//给Tab按钮设置背景
fragmentTabHost.getTabWidget().getChildAt(i).setBackgroundColor(getResources().getColor(R.color.main_bottom_bg));
//默认选中第一个tab
if(i == 0) {
tabItem.setChecked(true);
}
}
fragmentTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
//重置Tab样式
for (int i = 0; i< mTableItemList.size(); i++) {
TabItem tabitem = mTableItemList.get(i);
if (tabId.equals(tabitem.getTitleString())) {
tabitem.setChecked(true);
}else {
tabitem.setChecked(false);
}
}
}
});
}
private void initView() {
//测试栏目的题目统计TextView
mCount = (TextView) findViewById(R.id.tv_count);
mDoubleClickExit = new DoubleClickExitHelper(this);
Indicator[] indicators = Indicator.values();
mFragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mFragmentTabHost.setup(getApplicationContext(), getSupportFragmentManager(), R.id.realtabcontent);
//初始化Tab
for (int i = 0; i < indicators.length; i++){
TabHost.TabSpec tabSpec = mFragmentTabHost.newTabSpec(getString(indicators[i].getResName()));
tabSpec.setIndicator(getIndicatorView(indicators[i]));
mFragmentTabHost.addTab(tabSpec, indicators[i].getClz(), null);
}
//去除底部按钮之间的分割线
if (android.os.Build.VERSION.SDK_INT > 10) {
mFragmentTabHost.getTabWidget().setShowDividers(0);
mFragmentTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
if(tabId.equals(getString(Indicator.TEST.getResName()))){
mCount.setVisibility(View.VISIBLE);
}else{
mCount.setVisibility(View.GONE);
}
}
});
}}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_tab_activity);
ViewGroup viewGroup = (ViewGroup) findViewById(R.id.container);
mSimpleTab = new SimpleTab.Builder(this)
.newItem(new SimpleTab.Item().setLabelWithIcon(R.string.tab_1, R.drawable.ic_launcher))
.newItem(new SimpleTab.Item().setLabelWithIcon(R.string.tab_2, R.drawable.ic_launcher))
.newItem(new SimpleTab.Item().setLabelWithIcon(R.string.tab_3, R.drawable.ic_launcher))
.newItem(new SimpleTab.Item().setLabelWithIcon(R.string.tab_4, R.drawable.ic_launcher))
.setOnTabClickListener(new SimpleTab.OnTabClickListener() {
@Override
public void onItemClick(View view, SimpleTab.Item item, int position) {
Toast.makeText(CustomTabActivity.this, "position=" + position, Toast
.LENGTH_SHORT).show();
}
})
.create();
mSimpleTab.injectInto(viewGroup);
// TabWidget
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(
mTabHost.newTabSpec("tab1").setIndicator("Tab 1", null).setIndicator(getTabIndicator(this, R.string.tab_1, R.drawable.ic_launcher)),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab2").setIndicator("Tab 2", null).setIndicator
(getTabIndicator(this, R.string.tab_2, R.drawable.ic_launcher)),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab3").setIndicator("Tab 3", null).setIndicator
(getTabIndicator(this, R.string.tab_3, R.drawable.ic_launcher)),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab4").setIndicator("Tab 3", null).setIndicator
(getTabIndicator(this, R.string.tab_4, R.drawable.ic_launcher)),
FragmentTab.class, null);
}
@SuppressLint("NewApi")
private void initTabs() {
tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
tabHost.setOnTabChangedListener(this);
tabs = (TabWidget) findViewById(android.R.id.tabs);
// tabs.setShowDividers(TabWidget.SHOW_DIVIDER_NONE);
addTab(R.drawable.tab_icon_typography, TypographyContainer.class);
addTab(R.drawable.tab_icon_listview, ListViewContainer.class);
addTab(R.drawable.tab_icon_gridview, GridViewContainer.class);
addTab(R.drawable.tab_icon_buttons, ButtonsContainer.class);
addTab(R.drawable.tab_icon_icons, IconsContainer.class);
}
private void initViews(final View view) {
mOwnerImageView = (RoundedCornerImageView) view.findViewById(R.id.image_user);
mChatImageView = (ImageView) view.findViewById(R.id.chat_with_owner);
mChatImageView.setOnClickListener(this);
mOwnerNameTextView = (TextView) view.findViewById(R.id.text_user_name);
mOwnerBarterLocationTextView = (TextView) view
.findViewById(R.id.text_user_location);
mDragHandle = view.findViewById(R.id.container_profile_info);
mTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec(FragmentTags.ABOUT_ME)
.setIndicator(getString(R.string.about_me)), DummyFragment.class,
null
);
mTabHost.addTab(mTabHost.newTabSpec(FragmentTags.MY_BOOKS)
.setIndicator(getString(R.string.my_books)), DummyFragment.class,
null
);
mTabHost.setOnTabChangedListener(this);
mViewPager = (ViewPager) view.findViewById(R.id.pager_profile);
mProfileFragmentsAdapter = new ProfileFragmentsAdapter(getChildFragmentManager());
mViewPager.setAdapter(mProfileFragmentsAdapter);
mViewPager.setOnPageChangeListener(this);
}