android的TabActivity
前言
这段时间在研究android平台上的开源项目——StandupTimer,这是由jwood所设计的一个较为简单android应用,用于控制会议时间,类似秒表倒计时。
TabActivity & TabHost
tabActivity继承自Activity,其内部定义好了TabHost,可以通过getTabHost()获取。TabHost 包含了两种子元素:一些可以自由选择的Tab 和tab对应的内容tabContentto,在Layout的<TabHost>下它们分别对应 TabWidget和FrameLayout。
使用TabActivity可以让同一个界面容纳更多的内容。我们将按照Standup Timer里的TeamDetailsActivity来讲述TabActivity的使用。在该例中,包含了两个Tab一个用于展示team的统计信息,一个用于展示team所参加的会议的列表(这是一个ListView)。
创建Layout
这里需要注意的是不管你是使用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>
<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>
通常我们采用线性布局所以<TabHost> 的子元素是 <LinearLayout>。<TabWidger>对应Tab。<FrameLayout>则用于包含Tab需要展示的内容。需要注意的是<TabWidger> 和<FrameLayout>的Id 必须使用系统id,分别为android:id/tabs 和 android:id/tabcontent 。因为系统会使用者两个id来初始化TabHost的两个实例变量(mTabWidget 和 mTabContent)。
编写Java代码
我们可以采用两种方法编写标签页:一种是继承TabActivity ,然后使用getTabHost()获取TabHost对象;第二种方法是使用自定的TabHost在布局文件上<TabHost>的自定义其ID,然后通过findViewById(),方法获得TabHost对象。
本文中采用继承TabActivity的方法。
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);
}
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);
}
Java代码中我们首先需要做的是获取TabHost对象,可以通过TabActivtiy里的getTabHsot()方法。如果是自定义TabHost,在添加Tabs前 应该调用 setUp()方法。
mTabHost = (TabHost)findViewById(R.id.tabhost);
mTabHost.setup();
mTabHost.addTab(TAB_TAG_1, "Hello, world!", "Tab 1");
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
.
接着向TabHost添加tabs.即调用tabHost.addTab(TabSpec) 方法。TabSpec主要包含了setIndicator 和 setContent 方法,通过这两个方法来指定Tab 和 TanContent。
TabSpec 通过 .newTabSpec(String tag)来创建实例。实例化后对其属性进行设置 。setIndicator()设置tab,它有3个重载的函数
- public TabHost.TabSpec setIndicatior(CharSwquence label,Drawable icon).指定tab的标题和图标。
- public TabHost.TabSpec (View view)通过View来自定义tab
- public TabHost.TabSpec(CharSequence label) 指定tab的标题,此时无图标。
setContent 指定tab的展示内容,它也有3种重载
- public TabHost.TabSpec setContent(TabHost.TabContentFactory )
- public TabHost.TabSpec setContent(int ViewId)
- public TabHost.TabSpec setContent(Intent intent)
后两种方法比较后理解一个是通过 ViewId指定显示的内容,如.setContent(R.id.Team_EditText)。第三种则是直接通过Intent加载一个新的Activity页。如.setContent(new Intent(this, MeetingActivity.class)));
本例中是通过TabContentFactory 来指定对应的TabContent。TabContentFactory 是一个接口,其只包含了 一个返回 View 的createTabContent(String tag)方法。
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) {
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;
private ArrayAdapter<String> meetingListAdapter =null;
我们也可以让TabActivity去实现TabContentFactory 接口
publicclass Tabs2 extends TabActivity implements TabHost.TabContentFactory
然后在TabActiviy类中实现createTabContent方法
@Override
public View createTabContent(String tag) {
final TextView tv =new TextView(this);
tv.setText("Content for tab with tag "+ tag);
return tv;
}
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("");
}
}
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("");
}
}
最后将TabSpec 添加到 TabHost上,即tabHost.addTab(tabSpec)。我们发现TabSpec 的setIndicator 和 setContent 方法返回的都是 TabSpec 自身所以可以使用窜的方式编写代码:
tabhost.addTab(tabhost.newTabSpec("stats_tab")
.setIndicator(this.getString(R.string.stats))
.setContent(createMeetingDetails(team)));
.setIndicator(this.getString(R.string.stats))
.setContent(createMeetingDetails(team)));
其他参考资料
apiDemo也有3个例子可以参考。