主程序
InitActivity.java
package com.delightPress.chap61;
import com.delightPress.chap61.tabActivity.Income;
import com.delightPress.chap61.tabActivity.Outcome;
import com.delightPress.chap61.tabActivity.Totalcome;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
import com.delightPress.chap61.R;
public class InitActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); //object to get Drwable
TabHost tabHost = getTabHost(); //The activity TabHost
TabHost.TabSpec spec; //TabSpec for each tab
Intent intent; //Intent for each tab
//Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Income.class);
//初始化每一个标签的TabSpec,并且将它加入tabHost
spec = tabHost.newTabSpec("Income").setIndicator("Income",res.getDrawable(R.drawable.tab_icon1)).setContent(intent);
tabHost.addTab(spec);
//Do the same for the other tabs
intent = new Intent().setClass(this, Outcome.class);
spec = tabHost.newTabSpec("Outcome").setIndicator("Outcome",res.getDrawable(R.drawable.tab_icon1)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Totalcome.class);
spec = tabHost.newTabSpec("Total").setIndicator("Total",res.getDrawable(R.drawable.tab_icon1)).setContent(intent);
tabHost.addTab(spec);
//调用默认的标签
tabHost.setCurrentTab(2);
}
}
请求的对应的class都是加载标签中的图片和文字
main.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">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
</FrameLayout>
</LinearLayout>
</TabHost>
图片组合:
drawable文件夹下
tab_icon1.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 被选择时用高亮度的灰色 -->
<item android:drawable="@drawable/google_grey"
android:state_selected="true"></item>
<!-- 平时用透明底的白色 -->
<item android:drawable="@drawable/google_white" />
</selector>