Я только что разработал приложение с вкладками и использовал следующую функцию для создания вкладок в коде.Функция использует намерения, которые означают, что когда вы выбираете вкладку, активность загружается в тело вкладки с помощью намерения.Эта функция вызывается в основном действии.
private void SetupTabs()
{
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
//*****************************
intent = new Intent().setClass(MPaper.this, NewsList.class);
spec = tabHost.newTabSpec(NewsTag).setIndicator("News")
.setContent(intent);
tabHost.addTab(spec);
//*****************************
intent = new Intent().setClass(MPaper.this, Search.class);
spec = tabHost.newTabSpec(SearchTag).setIndicator("Search")
.setContent(intent);
tabHost.addTab(spec);
//****************************
intent = new Intent().setClass(MPaper.this, MyNewsList.class);
spec = tabHost.newTabSpec(MyNewsTag).setIndicator("My News")
.setContent(intent);
tabHost.addTab(spec);
//*****************************
intent = new Intent().setClass(MPaper.this, Extras.class);
spec = tabHost.newTabSpec(ExtrasTag).setIndicator("Extras")
.setContent(intent);
tabHost.addTab(spec);
//*****************************
tabHost.setCurrentTabByTag(NewsTag);
}
Представление списка в NewsList Activity выполняется с помощью XML следующим образом:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/NewsList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
В ответ на комментарий Фабиоса ниже: Да при использованиисвойство видимости или попробуйте реализовать вкладки, используя способ, указанный ниже:
private void TabSetup()
{
tabs = (TabHost)findViewById(R.id.tabhost);
tabs.setup();
TabHost.TabSpec spec = tabs.newTabSpec("MainNewsTag");
spec.setContent(R.id.MainNews);
spec.setIndicator("Home");
tabs.addTab(spec);
spec = tabs.newTabSpec("SearchTag");
spec.setContent(R.id.Search);
spec.setIndicator("Search");
tabs.addTab(spec);
spec = tabs.newTabSpec("MyNewsTag");
spec.setContent(R.id.MyNews);
spec.setIndicator("My News");
tabs.addTab(spec);
spec = tabs.newTabSpec("ExtrasTag");
spec.setContent(R.id.MyNews);
spec.setIndicator("Extras");
tabs.addTab(spec);
}
Где файл макета, как показано ниже:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TableLayout android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TabWidget android:id="@android:id/tabs"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
<FrameLayout android:id="@android:id/tabcontent"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
<ListView android:id="@+id/MainNews"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
.... and so on