这里需要注意的是不管你是使用TabActivity 还是自定义TabHost,都要求以TabHost作为XML布局文件的根。
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!--省略部分代码--> <TextView android:id="@+id/no_team_meetings" android:textSize="18sp" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <TextView android:id="@+id/no_team_meeting_stats" android:textSize="18sp" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </FrameLayout> </LinearLayout> </TabHost>
privatevoid createTabs() { TabHost tabhost=getTabHost(); tabhost.addTab(tabhost.newTabSpec("stats_tab"). setIndicator(this.getString(R.string.stats)). setContent(createMeetingDetails(team))); tabhost.addTab(tabhost.newTabSpec("meetings_tab"). setIndicator(this.getString(R.string.meetings)). setContent(createMeetingList())); getTabHost().setCurrentTab(0); }
mTabHost = (TabHost)findViewById(R.id.tabhost); mTabHost.setup(); mTabHost.addTab(TAB_TAG_1, "Hello, world!", "Tab 1");
SDK上的原文:
Call setup() before adding tabs if loading TabHost using findViewById(). However : You do not need to call setup() after getTabHost() in TabActivity
.
private TabContentFactory createMeetingDetails(Team team2) { returnnew TabHost.TabContentFactory() { @Override public View createTabContent(String tag) { //设置View setStatsTabContent(); return findViewById(R.id.teamStats); } }; } private TabHost.TabContentFactory createMeetingList() { returnnew TabHost.TabContentFactory() { @Override public View createTabContent(String tag) { meetingListAdapter = createMeetingListAdapter(); meetingList.setAdapter(meetingListAdapter); return meetingList; } }; }
private ListView meetingList=null; private ArrayAdapter<String> meetingListAdapter =null;
publicclass Tabs2 extends TabActivity implements TabHost.TabContentFactory
@Override public View createTabContent(String tag) { final TextView tv =new TextView(this); tv.setText("Content for tab with tag "+ tag); return tv; }
setStatsTabContent privatevoid setStatsTabContent() { if (team !=null&& team.hasMeetings(this)) { MeetingStats stats = team.getAverageMeetingStats(TeamDetailsActivity.this); ((TextView) findViewById(R.id.meeting_team_name_label)).setText(getString(R.string.team_name)); ((TextView) findViewById(R.id.meeting_team_name)).setText(team.getName()); ((TextView) findViewById(R.id.number_of_meetings_label)).setText(getString(R.string.number_of_meetings)); ((TextView) findViewById(R.id.number_of_meetings)).setText(Integer.toString((int) team.getNumberOfMeetings(TeamDetailsActivity.this))); ((TextView) findViewById(R.id.avg_number_of_participants_label)).setText(getString(R.string.avg_number_of_participants)); ((TextView) findViewById(R.id.avg_number_of_participants)).setText(Float.toString(stats.getNumParticipants())); ((TextView) findViewById(R.id.avg_meeting_length_label)).setText(getString(R.string.avg_meeting_length)); ((TextView) findViewById(R.id.avg_meeting_length)).setText(TimeFormatHelper.formatTime(stats.getMeetingLength())); ((TextView) findViewById(R.id.avg_individual_status_length_label)).setText(getString(R.string.avg_individual_status_length)); ((TextView) findViewById(R.id.avg_individual_status_length)).setText(TimeFormatHelper.formatTime(stats.getIndividualStatusLength())); ((TextView) findViewById(R.id.avg_quickest_status_label)).setText(getString(R.string.avg_quickest_status)); ((TextView) findViewById(R.id.avg_quickest_status)).setText(TimeFormatHelper.formatTime(stats.getQuickestStatus())); ((TextView) findViewById(R.id.avg_longest_status_label)).setText(getString(R.string.avg_longest_status)); ((TextView) findViewById(R.id.avg_longest_status)).setText(TimeFormatHelper.formatTime(stats.getLongestStatus())); } else { ((TextView) findViewById(R.id.meeting_team_name_label)).setText(getString(R.string.no_meeting_stats)); ((TextView) findViewById(R.id.meeting_team_name)).setText(""); ((TextView) findViewById(R.id.number_of_meetings_label)).setText(""); ((TextView) findViewById(R.id.number_of_meetings)).setText(""); ((TextView) findViewById(R.id.avg_number_of_participants_label)).setText(""); ((TextView) findViewById(R.id.avg_number_of_participants)).setText(""); ((TextView) findViewById(R.id.avg_meeting_length_label)).setText(""); ((TextView) findViewById(R.id.avg_meeting_length)).setText(""); ((TextView) findViewById(R.id.avg_individual_status_length_label)).setText(""); ((TextView) findViewById(R.id.avg_individual_status_length)).setText(""); ((TextView) findViewById(R.id.avg_quickest_status_label)).setText(""); ((TextView) findViewById(R.id.avg_quickest_status)).setText(""); ((TextView) findViewById(R.id.avg_longest_status_label)).setText(""); ((TextView) findViewById(R.id.avg_longest_status)).setText(""); } }
tabhost.addTab(tabhost.newTabSpec("stats_tab") .setIndicator(this.getString(R.string.stats)) .setContent(createMeetingDetails(team)));
博客原文地址:http://www.cnblogs.com/keyindex/articles/1815074.html