我对标签活动有点困惑,我在主视图上有3个标签,我可以使用标签切换视图,例如主要到课程或课程到主页,
但我想为每个标签调用不同的服务,例如
我目前的java和xml文件编码如下,
Java代码:
package com.NVT.android;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
public class MainTabActivity extends TabActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_activity_layout);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Main.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("main").setIndicator("Main",
res.getDrawable(R.drawable.ic_tab_artists_grey))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, Courses.class);
spec = tabHost.newTabSpec("courses").setIndicator("Courses",
res.getDrawable(R.drawable.ic_tab_artists_white))//m using same buttons for each tab u can change it by adding new drawable xml.. got that
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Courses.class);
spec = tabHost.newTabSpec("courses").setIndicator("Courses",
res.getDrawable(R.drawable.ic_tab_artists_white))//m using same buttons for each tab u can change it by adding new drawable xml.. got that
.setContent(intent);
tabHost.addTab(spec);
}
}
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"
android:padding="5dp">
<RelativeLayout
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"
android:layout_alignParentBottom="true"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"></FrameLayout>
</RelativeLayout>
</LinearLayout>
</TabHost>