編輯:Android開發實例
<?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> 的子元素是 <LinearLayout>。<TabWidger>對應Tab。<FrameLayout>則用於包含Tab需要展示的內容。需要注意的是<TabWidger> 和<FrameLayout>的Id 必須使用系統id,分別為android:id/tabs 和 android:id/tabcontent 。因為系統會使用者兩個id來初始化TabHost的兩個實例變量(mTabWidget 和 mTabContent)。
private void 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);
}
Java代碼中我們首先需要做的是獲取TabHost對象,可以通過TabActivtiy裡的getTabHsot()方法。如果是自定義TabHost,在添加Tabs前 應該調用 setUp()方法。
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) {
return new TabHost.TabContentFactory() {
@Override
public View createTabContent(String tag) {
//設置View
setStatsTabContent();
return findViewById(R.id.teamStats);
}
};
}
private TabHost.TabContentFactory createMeetingList()
{
return new TabHost.TabContentFactory() {
@Override
public View createTabContent(String tag) {
meetingListAdapter = createMeetingListAdapter();
meetingList.setAdapter(meetingListAdapter);
return meetingList;
}
};
}
事先聲明好的
private ListView meetingList=null;
private ArrayAdapter<String> meetingListAdapter = null;
我們也可以讓TabActivity去實現TabContentFactory 接口
public class 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;
}
setStatsTabContent();方法 setStatsTabContent
private void 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("");
}
}
最後將TabSpec 添加到 TabHost上,即tabHost.addTab(tabSpec)。我們發現TabSpec 的setIndicator 和 setContent 方法返回的都是 TabSpec 自身所以可以使用竄的方式編寫代碼:
tabhost.addTab(tabhost.newTabSpec("stats_tab")
.setIndicator(this.getString(R.string.stats))
.setContent(createMeetingDetails(team)));
JSON代表JavaScript對象符號。它是一個獨立的數據交換格式,是XML的最佳替代品。本章介紹了如何解析JSON文件,並從中提取所需的信息。Android提供了四個
Android提供了許多方法來控制播放的音頻/視頻文件和流。其中該方法是通過一類稱為MediaPlayer。Android是提供MediaPlayer類訪問內置的媒體播放
第一種方法的步驟: 1.在要Activity中實例化WebView組件:WebView webView = new WebView(this); 2.調用WebV
登錄應用程序的屏幕,詢問憑據登錄到一些特定的應用。可能需要登錄到Facebook,微博等本章介紹了,如何創建一個登錄界面,以及如何管理安全問題和錯誤嘗試。首先,必須定義兩